109 lines
2.7 KiB
JavaScript
109 lines
2.7 KiB
JavaScript
import * as themes from "themes";
|
|
import * as fs from "fs";
|
|
|
|
let defaultColorSet = [
|
|
'#666',
|
|
'#333'
|
|
]
|
|
|
|
function hslToRgb(h, s, l) {
|
|
var r, g, b;
|
|
|
|
if (s == 0) {
|
|
r = g = b = l; // achromatic
|
|
} else {
|
|
var hue2rgb = (p, q, t) => {
|
|
if(t < 0) t += 1;
|
|
if(t > 1) t -= 1;
|
|
if(t < 1/6) return p + (q - p) * 6 * t;
|
|
if(t < 1/2) return q;
|
|
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
|
|
return p;
|
|
}
|
|
|
|
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
var p = 2 * l - q;
|
|
r = hue2rgb(p, q, h + 1/3);
|
|
g = hue2rgb(p, q, h);
|
|
b = hue2rgb(p, q, h - 1/3);
|
|
}
|
|
|
|
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
|
|
}
|
|
|
|
function rgbToHex(r, g, b) {
|
|
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
|
}
|
|
|
|
const defaultRanges = [[0,1],[0.2,1],[0.2,0.8]];
|
|
|
|
var currentColorSet = defaultColorSet;
|
|
var colorPosition = [0, 0];
|
|
var colorReverse = [false, false];
|
|
|
|
export function setCurrentColorSet(colorSet) {
|
|
currentColorSet = colorSet;
|
|
}
|
|
|
|
function generateColors(hsl, variance, ranges, number) {
|
|
let colorArray = [];
|
|
for (let i=0; i<number; i++) {
|
|
colorArray.push(hslToHex(hsl, variance, ranges));
|
|
}
|
|
return colorArray;
|
|
}
|
|
|
|
function generateColorSet(colorDef, number) {
|
|
let colorSet = new Array([],[]);
|
|
for (let i=0; i<2; i++) {
|
|
if (typeof colorDef[i+1] === 'string') {
|
|
colorSet[i] = colorDef[i+1];
|
|
} else {
|
|
colorSet[i] = generateColors(colorDef[i+1].hsl, colorDef[i+1].variance, colorDef[i+1].ranges, number);
|
|
}
|
|
}
|
|
return colorSet;
|
|
}
|
|
|
|
export function initColors(colorDef, number) {
|
|
currentColorSet = generateColorSet(colorDef, number);
|
|
console.log('writing colors file');
|
|
fs.writeFileSync("colors.txt", currentColorSet, "cbor");
|
|
}
|
|
|
|
export function pickColor(type) {
|
|
let currentColor;
|
|
if (typeof currentColorSet[type] === 'string') {
|
|
currentColor = currentColorSet[type];
|
|
} else {
|
|
currentColor = currentColorSet[type][colorPosition[type]];
|
|
if (colorPosition[type] == currentColorSet[type].length-1) {
|
|
colorReverse[type] = true;
|
|
} else if (colorPosition[type] == 0) {
|
|
colorReverse[type] = false;
|
|
}
|
|
if (colorReverse[type]) {
|
|
colorPosition[type]--;
|
|
} else {
|
|
colorPosition[type]++;
|
|
}
|
|
}
|
|
return currentColor;
|
|
}
|
|
|
|
export function hslToHex(hsl, variance, ranges) {
|
|
let variance = variance || false;
|
|
let ranges = ranges || defaultRanges;
|
|
if (variance) {
|
|
for(let i=0; i<3; i++) {
|
|
hsl[i]+=Math.random()*variance[i]-(variance[i]/2);
|
|
if (hsl[i] < ranges[i][0]) {
|
|
hsl[i] = ranges[i][0];
|
|
} else if (hsl[i] > ranges[i][1]) {
|
|
hsl[i] = ranges[i][1];
|
|
}
|
|
}
|
|
}
|
|
let rgb = hslToRgb(hsl[0], hsl[1], hsl[2]);
|
|
return rgbToHex(rgb[0], rgb[1], rgb[2]);
|
|
} |