Created
April 27, 2015 08:35
-
-
Save xuan9/441ec38a5db683e8fa9f to your computer and use it in GitHub Desktop.
distinct colors. not good though
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var divs=[0,1/2,1/4,3/4,1/8,5/8,3/8,7/8]; | |
| function getDiv(index){ | |
| for(var i=divs.length;i<index;i++){ | |
| var root = Math.ceil(Math.log2(i+1)); | |
| var div=Math.pow(2, root); | |
| var lastDiv=Math.pow(2, root-1); | |
| var value=divs[i-lastDiv] + 1/div; | |
| divs[i]=value; | |
| } | |
| return divs[index-1]; | |
| } | |
| function getDistinctColor(index,startHue,startSaturation,startLight,hueRange,saturationRange,lightRange){ | |
| var div=getDiv(index); | |
| var hue = startHue + (hueRange[1]-hueRange[0])*div; | |
| if(hue>hueRange[1]){ | |
| hue = hue - hueRange[1]+hueRange[0]; | |
| } | |
| var saturation = startSaturation + (saturationRange[1]-saturationRange[0])*div; | |
| if(saturation>saturationRange[1]){ | |
| saturation = saturation - saturationRange[1]+saturationRange[0]; | |
| } | |
| var light = startLight + (lightRange[1]-lightRange[0])*div; | |
| if(light>lightRange[1]){ | |
| light = light - lightRange[1]+lightRange[0]; | |
| } | |
| // if( index % 2 === 0){ | |
| // saturation = saturationRange[0]+(saturationRange[1]-saturation); | |
| // } | |
| // if( index % 2 === 0){ | |
| // light = lightRange[0]+(lightRange[1]-light); | |
| // } | |
| return "hsl("+Math.round(hue)+", "+Math.round(saturation)+"%, "+Math.round(light)+"%)"; | |
| } | |
| function getDistinctColors(size,startHue,startSaturation,startLight,hueRange,saturationRange,lightRange){ | |
| var colors=[]; | |
| for(var i=0;i<size;i++){ | |
| colors[i]=getDistinctColor(i+1,startHue,startSaturation,startLight,hueRange,saturationRange,lightRange); | |
| } | |
| return colors; | |
| } | |
| function trimColorStr(colorStr){ | |
| return colorStr.replace(/[ ,()%]/g,''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment