Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 451cd59

Browse files
committed
Refactored Code
1 parent 867dfe1 commit 451cd59

File tree

2 files changed

+54
-60
lines changed

2 files changed

+54
-60
lines changed

Source/RGB.js

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,80 @@
1-
/*
2-
Copyright (c) 2021 JDK.FHWS@gmail.com
3-
*/
4-
51

62
const { min , max } = Math;
73

84

95
/*
10-
To Normalized
11-
*/
6+
* [ 0 - 255 ] ➞ [ 0 - 1 ]
7+
*/
128

13-
function toNormalized(byte){
14-
return byte / 255;
9+
function normalize(byte){
10+
return byte / 255;
1511
}
1612

1713

18-
/*
19-
To HSL
20-
*/
2114

2215
function toHSL(input){
2316

24-
const [ red , green , blue ] = input.map(toNormalized);
17+
const [ red , green , blue ] = input
18+
.map(normalize);
2519

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) ;
2923

30-
const
31-
chroma = maximum - minimun,
32-
lightness = (maximum + minimun) * .5;
24+
const
25+
chroma = maximum - minimun,
26+
lightness = (maximum + minimun) * .5;
3327

34-
return [ calcHue() , calcSaturation() , lightness ];
28+
return [ calcHue() , calcSaturation() * 100 , lightness * 100 ];
3529

3630

3731

38-
/*
39-
Calculate Hue
40-
*/
32+
function calcHue(){
4133

42-
function calcHue(){
34+
if(chroma === 0)
35+
return 0;
4336

44-
if(chroma === 0)
45-
return 0;
37+
let hue;
4638

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+
}
4844

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;
5446

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+
}
5652

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;
6255

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;
7158

59+
return hue;
60+
}
7261

73-
/*
74-
Calculate Saturation
75-
*/
7662

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;
8067

81-
return (maximum - lightness) / min(lightness,1 - lightness);
82-
}
68+
return (maximum - lightness) / min(lightness,1 - lightness);
69+
}
8370
}
8471

8572

8673
/*
87-
[ R , G , B ] -> [ H , S , L ]
88-
*/
74+
* [ R , G , B , (A) ] -> [ H , S , L , (A) ]
75+
*/
8976

90-
module.exports = (...colors) => {
77+
export default function fromRGB(colors){
9178

9279
if(colors.length === 1 || (typeof colors[2] !== 'number'))
9380
colors = colors[0];

Source/mod.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
/*
2-
Copyright (c) 2021 JDK.FHWS@gmail.com
3-
*/
41

2+
import convertRGB from './RGB.js'
53

6-
exports.rgbToHSL = require('./RGB.js');
4+
5+
/**
6+
* @brief Converts an RGB color to HSL
7+
* @param rgb Either ( R , G , B ) or ([ R , G , B ])
8+
* @note Alpha channel can be included and won't be altered.
9+
*/
10+
11+
export function fromRGB ( ... rgb : number [] | number[][] ) : number [] {
12+
return convertRGB ( rgb ) as number [];
13+
}

0 commit comments

Comments
 (0)