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

Commit aa36702

Browse files
committed
Refactored code
1 parent df61039 commit aa36702

File tree

14 files changed

+282
-142
lines changed

14 files changed

+282
-142
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
root = true
4+
5+
6+
[*]
7+
insert_final_newline = true
8+
end_of_line = lf
9+
10+
11+
[*.{md,js}]
12+
indent_style = space
13+
indent_size = 4
14+
charset = utf-8

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

Source/HSL.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

Source/Hex.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

Source/Module.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

Source/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
<br>
3+
4+
<div align = center>
5+
6+
# HSL
7+
8+
*Convert colors to RGB.*
9+
10+
<br>
11+
12+
[![Button Documentation]][Documentation]
13+
14+
<div>
15+
16+
<br>
17+
18+
19+
<!----------------------------------------------------------------------------->
20+
21+
[Documentation]: https://github.com/OmegaTools/RGB
22+
23+
24+
<!---------------------------------[ Buttons ]--------------------------------->
25+
26+
[Button Documentation]: https://img.shields.io/badge/Documentation-04ACE6?style=for-the-badge&logoColor=white&logo=GitHub

Source/Sources/HSL.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
const { min , max , round } = Math;
3+
4+
5+
const offsets = [ 0 , 8 , 4 ];
6+
7+
8+
function toRGB([ Hue , Saturation , Lightness ]){
9+
10+
Saturation *= 0.01;
11+
Lightness *= 0.01;
12+
13+
// To Degree
14+
15+
Hue *= 1 / 30;
16+
17+
const a = Saturation * min(Lightness,1 - Lightness);
18+
19+
return offsets.map((offset) => {
20+
21+
offset += Hue;
22+
23+
// Sector -> Sector Index
24+
25+
offset %= 12;
26+
27+
// Nearest Sector
28+
29+
offset = min(offset - 3,9 - offset,1);
30+
31+
// Limit Sectors
32+
33+
offset = max(-1,offset);
34+
35+
// To Color
36+
37+
offset *= a;
38+
39+
// Correct
40+
41+
offset = Lightness - offset;
42+
43+
// To 265
44+
45+
offset *= 255;
46+
47+
// To Int
48+
49+
return round(offset);
50+
});
51+
}
52+
53+
54+
/*
55+
* [ H , S , L , (A) ] -> [ R , G , B , (A) ]
56+
*/
57+
58+
export default function fromHSL(colors){
59+
60+
if(colors.length === 1 || (typeof colors[2] !== 'number'))
61+
colors = colors[0];
62+
63+
const rgb = toRGB(colors);
64+
65+
const alpha = colors[3];
66+
67+
if(alpha)
68+
rgb.push(alpha);
69+
70+
return rgb;
71+
}

Source/Sources/Hex.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
const { errors } = Deno;
4+
const { log } = console;
5+
6+
const
7+
channels_single = /^([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})?$/i ,
8+
channels_double = /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i ;
9+
10+
const invalid_hex_string =
11+
'Invalid hex color string format, must be (#)RRGGBB(AA) or (#)RGB(A)';
12+
13+
const parseHex = (string) =>
14+
parseInt(string,16);
15+
16+
17+
/*
18+
* (#)RRGGBB(AA) or (#)RGB(A) -> [ R , G , B , (A) ]
19+
*/
20+
21+
export default function fromHex(hex){
22+
23+
if(hex.startsWith('#'))
24+
hex = hex.substring(1);
25+
26+
const { length } = hex;
27+
28+
const matches =
29+
hex.match(channels_double) ??
30+
hex.match(channels_single) ;
31+
32+
if(!matches)
33+
throw new InvalidData(invalid_hex_string);
34+
35+
const channels = matches
36+
.slice(1)
37+
.filter((value) => value)
38+
.map(parseHex);
39+
40+
return channels;
41+
}

Source/mod.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import HSL from './Sources/HSL.js'
3+
import Hex from './Sources/Hex.js'
4+
5+
6+
/**
7+
* Convert a Hex color string to RGB
8+
*
9+
* Alpha channel can be included and won't be altered.
10+
*
11+
* @param color (#)RRGGBB(AA) or (#)RGB(A)
12+
* @return An RGB(A) color array
13+
*/
14+
15+
export function fromHex (
16+
color : string
17+
) : number [] {
18+
return Hex( color ) as number [];
19+
}
20+
21+
22+
/**
23+
* Convert an HSL color to RGB
24+
*
25+
* Alpha channel can be included and won't be altered.
26+
*
27+
* @param color Either ( H , S , L , (A) ) or ([ H , S , L , (A) ])
28+
* @return A HSL(A) color array
29+
*/
30+
31+
export function fromHSL (
32+
... color : number [] | number[][]
33+
) : number [] {
34+
return HSL( color ) as number [];
35+
}

Tests/AssertColor.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
import { fromHex , fromHSL } from 'RGB'
4+
import { assertEquals } from 'Assert'
5+
6+
7+
export function assertHexIsRGB(input,output){
8+
assertEquals(fromHex(input),output);
9+
}
10+
11+
12+
export function assertHSLIsRGB(input,output){
13+
assertEquals(fromHSL(input),output);
14+
}

0 commit comments

Comments
 (0)