|
1 |
| -/* |
2 |
| - Copyright (c) 2021 JDK.FHWS@gmail.com |
3 |
| -*/ |
4 |
| - |
5 | 1 |
|
6 | 2 | const { min , max } = Math;
|
7 | 3 |
|
8 | 4 |
|
9 | 5 | /*
|
10 |
| - To Normalized |
11 |
| -*/ |
| 6 | + * [ 0 - 255 ] ➞ [ 0 - 1 ] |
| 7 | + */ |
12 | 8 |
|
13 |
| -function toNormalized(byte){ |
14 |
| - return byte / 255; |
| 9 | +function normalize(byte){ |
| 10 | + return byte / 255; |
15 | 11 | }
|
16 | 12 |
|
17 | 13 |
|
18 |
| -/* |
19 |
| - To HSL |
20 |
| -*/ |
21 | 14 |
|
22 | 15 | function toHSL(input){
|
23 | 16 |
|
24 |
| - const [ red , green , blue ] = input.map(toNormalized); |
| 17 | + const [ red , green , blue ] = input |
| 18 | + .map(normalize); |
25 | 19 |
|
26 |
| - const |
27 |
| - minimun = min(red,green,blue), |
28 |
| - maximum = max(red,green,blue); |
| 20 | + const |
| 21 | + minimun = min(red,green,blue) , |
| 22 | + maximum = max(red,green,blue) ; |
29 | 23 |
|
30 |
| - const |
31 |
| - chroma = maximum - minimun, |
32 |
| - lightness = (maximum + minimun) * .5; |
| 24 | + const |
| 25 | + chroma = maximum - minimun, |
| 26 | + lightness = (maximum + minimun) * .5; |
33 | 27 |
|
34 |
| - return [ calcHue() , calcSaturation() , lightness ]; |
| 28 | + return [ calcHue() , calcSaturation() * 100 , lightness * 100 ]; |
35 | 29 |
|
36 | 30 |
|
37 | 31 |
|
38 |
| - /* |
39 |
| - Calculate Hue |
40 |
| - */ |
| 32 | + function calcHue(){ |
41 | 33 |
|
42 |
| - function calcHue(){ |
| 34 | + if(chroma === 0) |
| 35 | + return 0; |
43 | 36 |
|
44 |
| - if(chroma === 0) |
45 |
| - return 0; |
| 37 | + let hue; |
46 | 38 |
|
47 |
| - let hue; |
| 39 | + switch(maximum){ |
| 40 | + case red : hue = (green - blue); break; |
| 41 | + case green : hue = (blue - red); break; |
| 42 | + case blue : hue = (red - green); break; |
| 43 | + } |
48 | 44 |
|
49 |
| - switch(maximum){ |
50 |
| - case red : hue = (green - blue); break; |
51 |
| - case green : hue = (blue - red); break; |
52 |
| - case blue : hue = (red - green); break; |
53 |
| - } |
| 45 | + hue /= chroma; |
54 | 46 |
|
55 |
| - hue /= chroma; |
| 47 | + switch(maximum){ |
| 48 | + case red : hue += 0; break; |
| 49 | + case green : hue += 2; break; |
| 50 | + case blue : hue += 4; break; |
| 51 | + } |
56 | 52 |
|
57 |
| - switch(maximum){ |
58 |
| - case red : hue += 0; break; |
59 |
| - case green : hue += 2; break; |
60 |
| - case blue : hue += 4; break; |
61 |
| - } |
| 53 | + hue *= 60; |
| 54 | + hue %= 360; |
62 | 55 |
|
63 |
| - hue *= 60; |
64 |
| - hue %= 360; |
65 |
| - |
66 |
| - if(hue < 0) |
67 |
| - hue += 360; |
68 |
| - |
69 |
| - return hue; |
70 |
| - } |
| 56 | + if(hue < 0) |
| 57 | + hue += 360; |
71 | 58 |
|
| 59 | + return hue; |
| 60 | + } |
72 | 61 |
|
73 |
| - /* |
74 |
| - Calculate Saturation |
75 |
| - */ |
76 | 62 |
|
77 |
| - function calcSaturation(){ |
78 |
| - if([ 0 , 1 ].includes(lightness)) |
79 |
| - return 0; |
| 63 | + function calcSaturation(){ |
| 64 | + |
| 65 | + if([ 0 , 1 ].includes(lightness)) |
| 66 | + return 0; |
80 | 67 |
|
81 |
| - return (maximum - lightness) / min(lightness,1 - lightness); |
82 |
| - } |
| 68 | + return (maximum - lightness) / min(lightness,1 - lightness); |
| 69 | + } |
83 | 70 | }
|
84 | 71 |
|
85 | 72 |
|
86 | 73 | /*
|
87 |
| - [ R , G , B ] -> [ H , S , L ] |
88 |
| -*/ |
| 74 | + * [ R , G , B , (A) ] -> [ H , S , L , (A) ] |
| 75 | + */ |
89 | 76 |
|
90 |
| -module.exports = (...colors) => { |
| 77 | +export default function fromRGB(colors){ |
91 | 78 |
|
92 | 79 | if(colors.length === 1 || (typeof colors[2] !== 'number'))
|
93 | 80 | colors = colors[0];
|
|
0 commit comments