This repository was archived by the owner on Mar 14, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +168
-2
lines changed
Expand file tree Collapse file tree 7 files changed +168
-2
lines changed Original file line number Diff line number Diff 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
3552All channels are decimals of ` 0 - 100 ` <br >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+
2+ import fromRGB from './RGB.js'
3+
4+
5+ const { errors } = Deno ;
6+ const { log } = console ;
7+
8+ const
9+ channels_single = / ^ ( [ 0 - 9 a - f ] { 1 } ) ( [ 0 - 9 a - f ] { 1 } ) ( [ 0 - 9 a - f ] { 1 } ) ( [ 0 - 9 a - f ] { 1 } ) ? $ / i ,
10+ channels_double = / ^ ( [ 0 - 9 a - f ] { 2 } ) ( [ 0 - 9 a - f ] { 2 } ) ( [ 0 - 9 a - f ] { 2 } ) ( [ 0 - 9 a - 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+ }
Original file line number Diff line number Diff line change 11
22import CMYK from './Sources/CMYK.js'
33import 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 *
Original file line number Diff line number Diff line change 11
22
3- import { fromCMYK , fromRGB } from 'HSL'
3+ import { fromCMYK , fromHex , fromRGB } from 'HSL'
44import { assertEquals } from 'Assert'
55
66
@@ -12,3 +12,8 @@ export function assertRGBIsHSL(input,output){
1212export function assertCMYKIsHSL ( input , output ) {
1313 assertEquals ( fromCMYK ( input ) , output ) ;
1414}
15+
16+
17+ export function assertHexIsHSL ( input , output ) {
18+ assertEquals ( fromHex ( input ) , output ) ;
19+ }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments