diff --git a/index.js b/index.js index 6e8ccd3..2852191 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ import * as Vector from './js/vectorlib.js'; import * as Bezier from './js/bezierlib.js'; import * as Utils from './js/utillib.js'; +import {Color} from './js/colorlib.js'; import * as Config from './config.js'; // CONFIG @@ -331,7 +332,6 @@ function fillSvg(){ /** Insert individual rail paths, as well as creating signage data for each path*/ function drawTracks(bezierData){ - const trackCol = Utils.rgba2hex(Math.random(),Math.random(),Math.random(),1.0); if(trackNameCounting[bezierData.name] == null){ trackNameCounting[bezierData.name] = 0; }else{ @@ -354,7 +354,7 @@ function drawTracks(bezierData){ }else if(/\[track (diverging|through)\]/.test(bezierData.name)){ bezierData.isPoint = true; } - bezierData.randomColor = Utils.rgba2hex(Math.random(), Math.random(), Math.random(), 1); + bezierData.randomColor = Color.random(); for(let i=0; i+1= 1) return colorB; + let output = new Color(); + output.r = colorA.r*(1-factor) + colorB.r*factor; + output.g = colorA.g*(1-factor) + colorB.g*factor; + output.b = colorA.b*(1-factor) + colorB.b*factor; + output.a = colorA.a*(1-factor) + colorB.a*factor; + return output; + } + + /** + * Blend along a gradient of colors + * @param {Color[]} colorList + * @param {number} factor 0-1 blend along `colorList` + * @param {string} colorspace + */ + static blendGradient(colorList, factor, colorspace = 'srgb'){ + if(colorList.length == 0) return new Color(); + if(colorList.length == 1 || factor <= 0) return colorList[0]; + if(factor >= 1) return colorList[colorList.length-1]; + + let factorIndex = (colorList.length-1) * factor; + switch(colorspace.toLowerCase()){ + default: + return this.blendSrgb(colorList[Math.floor(factorIndex)], colorList[Math.ceil(factorIndex)], factorIndex % 1); + } + } +} \ No newline at end of file diff --git a/js/utillib.js b/js/utillib.js index a9ad914..f74ac3e 100644 --- a/js/utillib.js +++ b/js/utillib.js @@ -17,22 +17,6 @@ export function randomRange(min=0.0, max=1.0){ return Math.random()*(max-min) + min; } -export function rgba2hex(r,g,b,a=1.0){ - r = Math.min(Math.max(r,0),1); - g = Math.min(Math.max(g,0),1); - b = Math.min(Math.max(b,0),1); - a = Math.min(Math.max(a,0),1); - return `#${ - Math.round(r*255).toString(16).padStart(2, '0').substring(0,2) - }${ - Math.round(g*255).toString(16).padStart(2, '0').substring(0,2) - }${ - Math.round(b*255).toString(16).padStart(2, '0').substring(0,2) - }${ - Math.round(a*255).toString(16).padStart(2, '0').substring(0,2) - }`; -} - export function gradeToClass(grade){ for(let j=0; j gradeIncrements[j]){