parent
81ae2c7e29
commit
dc6d69f92a
4 changed files with 100 additions and 20 deletions
@ -0,0 +1,41 @@ |
|||||||
|
export function rgbToCss(rgbArray){ |
||||||
|
const r255 = Math.round(rgbArray[0] * 255); |
||||||
|
const g255 = Math.round(rgbArray[1] * 255); |
||||||
|
const b255 = Math.round(rgbArray[2] * 255); |
||||||
|
return `rgb(${r255}, ${g255}, ${b255})`; |
||||||
|
} |
||||||
|
|
||||||
|
export function getRgbFromIndex(index, length){ |
||||||
|
const hue = (index / length) * 360; |
||||||
|
const saturation = 1; |
||||||
|
const lightness = 0.5; |
||||||
|
return hslToRgb(hue, saturation, lightness); |
||||||
|
} |
||||||
|
|
||||||
|
function hslToRgb(h, s, l) { |
||||||
|
let r, g, b; |
||||||
|
// normalize h to [0,1] if provided in degrees (0..360)
|
||||||
|
let hh = h; |
||||||
|
if (hh > 1) hh = hh / 360; |
||||||
|
|
||||||
|
if (s === 0) { |
||||||
|
r = g = b = l; // achromatic
|
||||||
|
} else { |
||||||
|
const 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; |
||||||
|
}; |
||||||
|
|
||||||
|
const q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
||||||
|
const p = 2 * l - q; |
||||||
|
|
||||||
|
r = hue2rgb(p, q, hh + 1 / 3); |
||||||
|
g = hue2rgb(p, q, hh); |
||||||
|
b = hue2rgb(p, q, hh - 1 / 3); |
||||||
|
} |
||||||
|
return [r, g, b]; |
||||||
|
} |
||||||
Loading…
Reference in new issue