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

Commit 0661311

Browse files
committed
Added CMYK Conversion
1 parent 86a5cc4 commit 0661311

File tree

9 files changed

+227
-54
lines changed

9 files changed

+227
-54
lines changed

Documentation/Conversions.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ All channels are integers in the range of  `0 - 255`
2828
| **Parameter** | ( R , G , B ) | ( R , G , B , A )
2929

3030
<br>
31+
<br>
32+
33+
## CMYK ( A )
34+
35+
All channels are decimals of  `0 - 100`  <br>
36+
except for the alpha channel  `0 - 255`
37+
38+
<br>
39+
40+
| | CMYK | CMYKA |
41+
|:-:|:----:|:-----:|
42+
| **Array** | ( [ C , M , Y , K ] ) | ( [ C , M , Y , K , A ] )
43+
| **Parameter** | ( C , M , Y , K ) | ( C , M , Y , K , A )
44+
45+
<br>

Documentation/Sources/CMYK.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# CMYK
3+
4+
*Convert CMYK colors to HSL.*
5+
6+
```JavaScript
7+
import { fromCMYK } from 'HSL'
8+
```
9+
10+
<br>
11+
12+
## Parameter
13+
14+
*Supply the channels directly.*
15+
16+
```JavaScript
17+
const cmyk = fromCMYK( C , M , Y , K );
18+
```
19+
20+
<br>
21+
22+
## Array
23+
24+
*Pass the channels as an array.*
25+
26+
```JavaScript
27+
const cmyk = fromCMYK([ C , M , Y , K ]);
28+
```
29+
30+
<br>
31+
32+
## Alpha
33+
34+
*Optionally include an Alpha channel.*
35+
36+
```JavaScript
37+
const cmyka = fromCMYK([ C , M , Y , K , A ]);
38+
```
39+
40+
```JavaScript
41+
const cmyka = fromCMYK( C , M , Y , K , A );
42+
```
43+
44+
<br>

Documentation/Sources/RGB.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# RGB
3+
4+
*Convert RGB colors to HSL.*
5+
6+
```JavaScript
7+
import { fromRGB } from 'HSL'
8+
```
9+
10+
<br>
11+
12+
## Parameter
13+
14+
*Supply the channels directly.*
15+
16+
```JavaScript
17+
const hsl = fromRGB( R , G , B );
18+
```
19+
20+
<br>
21+
22+
## Array
23+
24+
*Pass the channels as an array.*
25+
26+
```JavaScript
27+
const hsl = fromRGB([ R , G , B ]);
28+
```
29+
30+
<br>
31+
32+
## Alpha
33+
34+
*Optionally include an Alpha channel.*
35+
36+
```JavaScript
37+
const hsla = fromRGB([ R , G , B , A ]);
38+
```
39+
40+
```JavaScript
41+
const hsla = fromRGB( R , G , B , A );
42+
```
43+
44+
<br>

Documentation/Usage.md

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11

2+
<br>
3+
4+
<div align = center>
5+
26
# Usage
37

48
*How to this module.*
59

10+
<br>
11+
<br>
12+
13+
14+
15+
[![Button RGB]][RGB]   
16+
[![Button CMYK]][CMYK]
17+
18+
</div>
19+
20+
<br>
621
<br>
722

823
## Import
@@ -25,49 +40,16 @@ import * as HSL from 'https://deno.land/x/hsl/mod.ts'
2540
}
2641
```
2742

28-
<br>
2943
<br>
3044

31-
## RGB
3245

33-
*Convert RGB colors to HSL.*
46+
<!----------------------------------------------------------------------------->
3447

35-
```JavaScript
36-
import { fromRGB } from 'HSL'
37-
```
48+
[CMYK]: Sources/CMYK.md
49+
[RGB]: Sources/RGB.md
3850

39-
<br>
4051

41-
### As Parameter
42-
43-
*Supply the channels directly.*
44-
45-
```JavaScript
46-
const hsl = fromRGB( R , G , B );
47-
```
52+
<!---------------------------------[ Buttons ]--------------------------------->
4853

49-
<br>
50-
51-
### As Array
52-
53-
*Pass the channels as an array.*
54-
55-
```JavaScript
56-
const hsl = fromRGB([ R , G , B ]);
57-
```
58-
59-
<br>
60-
61-
### With Alpha
62-
63-
*Optionally include an Alpha channel.*
64-
65-
```JavaScript
66-
const hsla = fromRGB([ R , G , B , A ]);
67-
```
68-
69-
```JavaScript
70-
const hsla = fromRGB( R , G , B , A );
71-
```
72-
73-
<br>
54+
[Button CMYK]: https://img.shields.io/badge/CMYK-04ACE6?style=for-the-badge
55+
[Button RGB]: https://img.shields.io/badge/RGB-37814A?style=for-the-badge

Source/Sources/CMYK.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import fromRGB from './RGB.js'
3+
4+
5+
function toHSL(channels){
6+
7+
const [ C , M , Y , K ] = channels
8+
.map((channel) => channel / 100)
9+
.map((channel) => 1 - channel)
10+
11+
12+
return fromRGB([
13+
255 * C * K ,
14+
255 * M * K ,
15+
255 * Y * K
16+
])
17+
}
18+
19+
20+
/*
21+
* [ C , M , Y , K , (A) ] -> [ H , S , L , (A) ]
22+
*/
23+
24+
export default function fromCMYK(colors){
25+
26+
27+
if(colors.length === 1 || (typeof colors[2] !== 'number'))
28+
colors = colors[0];
29+
30+
const hsl = toHSL(colors);
31+
32+
const alpha = colors[4];
33+
34+
if(alpha)
35+
hsl.push(alpha);
36+
37+
return hsl;
38+
}

Source/RGB.js renamed to Source/Sources/RGB.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ function toHSL(input){
7676

7777
export default function fromRGB(colors){
7878

79-
if(colors.length === 1 || (typeof colors[2] !== 'number'))
80-
colors = colors[0];
79+
if(colors.length === 1 || (typeof colors[2] !== 'number'))
80+
colors = colors[0];
8181

82-
const hsl = toHSL(colors);
82+
const hsl = toHSL(colors);
8383

84-
const alpha = colors[3];
84+
const alpha = colors[3];
8585

86-
if(alpha)
87-
hsl.push(alpha);
86+
if(alpha)
87+
hsl.push(alpha);
8888

89-
return hsl;
89+
return hsl;
9090
}

Source/mod.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11

2-
import convertRGB from './RGB.js'
2+
import CMYK from './Sources/CMYK.js'
3+
import RGB from './Sources/RGB.js'
34

45

56
/**
6-
* @brief Converts an RGB color to HSL
7-
* @param rgb Either ( R , G , B ) or ([ R , G , B ])
7+
* @brief Converts a RGB color to HSL
8+
* @param color Either ( R , G , B ) or ([ R , G , B ])
89
* @note Alpha channel can be included and won't be altered.
910
*/
1011

11-
export function fromRGB ( ... rgb : number [] | number[][] ) : number [] {
12-
return convertRGB ( rgb ) as number [];
12+
export function fromRGB ( ... color : number [] | number[][] ) : number [] {
13+
return RGB( color ) as number [];
14+
}
15+
16+
17+
/**
18+
* @brief Converts a CMYK color to HSL
19+
* @param color Either ( C , M , Y , B ) or ([ R , G , B ])
20+
* @note Alpha channel can be included and won't be altered.
21+
*/
22+
23+
export function fromCMYK ( ... color : number [] | number[][] ) : number [] {
24+
return CMYK( color ) as number [];
1325
}

Tests/AssertColor.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22

3+
import { fromCMYK , fromRGB } from 'HSL'
34
import { assertEquals } from 'Assert'
4-
import { fromRGB } from 'HSL'
55

66

7-
export function assertRGBIsHSL(rgb,hsl){
8-
assertEquals(fromRGB(rgb),hsl);
7+
export function assertRGBIsHSL(input,output){
8+
assertEquals(fromRGB(input),output);
9+
}
10+
11+
12+
export function assertCMYKIsHSL(input,output){
13+
assertEquals(fromCMYK(input),output);
914
}

Tests/Sources/CMYK.js

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

0 commit comments

Comments
 (0)