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

Commit f7887c8

Browse files
committed
Add hex conversion
1 parent 164ee9d commit f7887c8

File tree

7 files changed

+168
-2
lines changed

7 files changed

+168
-2
lines changed

Documentation/Conversions.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ All channels are integers in the range of  `0 - 255`
3030
<br>
3131
<br>
3232

33+
## Hex ( + Alpha )
34+
35+
All channels are integers in the range of  `0 - FF`
36+
37+
<br>
38+
39+
| | Hex | Hex (Alpha) |
40+
|:-:|:---:|:-----------:|
41+
| **Single** | `#RGB` | `#RGBA`
42+
| **Double** | `#RRGGBB` | `#RRGGBBAA`
43+
44+
*`#`  is optional.*
45+
46+
<br>
47+
<br>
48+
49+
3350
## CMYK ( A )
3451

3552
All channels are decimals of  `0 - 100`  <br>

Documentation/Sources/Hex.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# Hex
3+
4+
*Convert Hex color strings to HSL.*
5+
6+
```JavaScript
7+
import { fromHex } from 'HSL'
8+
```
9+
10+
<br>
11+
12+
## Single & Double
13+
14+
*Use either format to specify the RGB parts.*
15+
16+
```JavaScript
17+
const hsl = fromHex('RGB');
18+
```
19+
20+
```JavaScript
21+
const hsl = fromHex('RRGGBB');
22+
```
23+
24+
<br>
25+
26+
## Hashtag
27+
28+
*Optionally place the  `#`  before it.*
29+
30+
```JavaScript
31+
const hsl = fromHex('#RRGGBB');
32+
```
33+
34+
<br>
35+
36+
## Alpha
37+
38+
*Optionally include an Alpha channel.*
39+
40+
```JavaScript
41+
const hsla = fromHex('RGBA');
42+
```
43+
44+
```JavaScript
45+
const hsla = fromHex('RRGGBBAA');
46+
```
47+
48+
<br>

Documentation/Usage.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<br>
1212

1313
[![Button RGB]][RGB]   
14-
[![Button CMYK]][CMYK]
14+
[![Button CMYK]][CMYK]   
15+
[![Button Hex]][Hex]
1516

1617
</div>
1718

@@ -47,9 +48,11 @@ import * as HSL from 'https://deno.land/x/hsl/mod.ts'
4748

4849
[CMYK]: Sources/CMYK.md
4950
[RGB]: Sources/RGB.md
51+
[Hex]: Sources/Hex.md
5052

5153

5254
<!---------------------------------[ Buttons ]--------------------------------->
5355

5456
[Button CMYK]: https://img.shields.io/badge/CMYK-04ACE6?style=for-the-badge
5557
[Button RGB]: https://img.shields.io/badge/RGB-37814A?style=for-the-badge
58+
[Button Hex]: https://img.shields.io/badge/Hex-EF2D5E?style=for-the-badge

Source/Sources/Hex.js

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

Source/mod.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import CMYK from './Sources/CMYK.js'
33
import RGB from './Sources/RGB.js'
4+
import Hex from './Sources/Hex.js'
45

56

67
/**
@@ -19,6 +20,22 @@ export function fromRGB (
1920
}
2021

2122

23+
/**
24+
* Convert a Hex color string to HSL
25+
*
26+
* Alpha channel can be included and won't be altered.
27+
*
28+
* @param color (#)RRGGBB(AA) or (#)RGB(A)
29+
* @return A HSL(A) color array
30+
*/
31+
32+
export function fromHex (
33+
color : string
34+
) : number [] {
35+
return Hex( color ) as number [];
36+
}
37+
38+
2239
/**
2340
* Convert a CMYK color to HSL
2441
*

Tests/AssertColor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
import { fromCMYK , fromRGB } from 'HSL'
3+
import { fromCMYK , fromHex , fromRGB } from 'HSL'
44
import { assertEquals } from 'Assert'
55

66

@@ -12,3 +12,8 @@ export function assertRGBIsHSL(input,output){
1212
export function assertCMYKIsHSL(input,output){
1313
assertEquals(fromCMYK(input),output);
1414
}
15+
16+
17+
export function assertHexIsHSL(input,output){
18+
assertEquals(fromHex(input),output);
19+
}

Tests/Sources/Hex.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import { assertHexIsHSL } from 'AssertColor'
3+
4+
const { test } = Deno;
5+
6+
7+
test('Hex String Conversion',() => {
8+
9+
// Black
10+
11+
assertHexIsHSL('#000000',[ 0 , 0 , 0 ]);
12+
13+
14+
// White
15+
16+
assertHexIsHSL('#FFFFFF',[ 0 , 0 , 100 ]);
17+
18+
19+
// Red
20+
21+
assertHexIsHSL('#FF0000',[ 0 , 100 , 50 ]);
22+
23+
24+
// Green
25+
26+
assertHexIsHSL('#00FF00',[ 120 , 100 , 50 ]);
27+
28+
29+
// Blue
30+
31+
assertHexIsHSL('#0000FF',[ 240 , 100 , 50 ]);
32+
33+
})

0 commit comments

Comments
 (0)