/
hwb-space.ts
100 lines (90 loc) · 2.85 KB
/
hwb-space.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import HWBColorSpace from './types/hwb-color-space';
import hwbConverter from '../utils/converter/hwb-converter';
import { setHueColorSpaceValue } from '../utils/hue-utils';
import AbstractSpace from '../abstract-space';
/**
* HWB wrapper which provides mutations and accessor functions for
* the HWB color space.
*
* @public
*/
export default class HWBSpace extends AbstractSpace<'hwb', HWBColorSpace> {
constructor(model: HWBColorSpace) {
super('hwb', model, hwbConverter);
}
/**
* Retrieves the value of the hue channel for the current color space.
*
* @returns {number} the hue channel value of this color space
*/
public hue(): number {
return this.model.hue;
}
/**
* Retrieves the value of the whiteness channel for the current color space.
*
* @returns {number} the whiteness channel value of this color space
*/
public whiteness(): number {
return this.model.whiteness;
}
/**
* Retrieves the value of the blackness channel for the current color space.
*
* @returns {number} the blackness channel value of this color space
*/
public blackness(): number {
return this.model.blackness;
}
/**
* Clones the current color space.
*
* @return a new cloned instance of the original color space
*/
public clone(): HWBSpace {
return new HWBSpace({ ...this.model });
}
public setColor(color: keyof HWBColorSpace, value: number): HWBSpace {
const adjusted = setHueColorSpaceValue(this.model, color, value);
return this.applyModel(adjusted);
}
/**
* Retrieves an array representing the HWB color space containing the primary colors.
*
* @remarks Array index is ordered logically [HWB]
*
* @return {[number, number, number]} the HWB color space values as an array
*/
public toArray(): [number, number, number] {
return [this.model.hue, this.model.whiteness, this.model.blackness];
}
/**
* Prints valid CSS string value.
*
* @example
* Here's a simple usage example:
* ```ts
* color.toString() // hwb(25,40%,54%)
* ```
*
* @return {string} valid CSS color value.
*/
public toString(): string {
return `hwb(${Math.round(this.model.hue)},${Math.round(
this.model.whiteness
)}%,${Math.round(this.model.blackness)}%)`;
}
/* ---------- PRIVATE FUNCTIONS --------- */
public applyModel(space: HWBColorSpace): HWBSpace {
this.model.hue = Math.min(Math.max(Math.floor(space.hue), 0), 360);
this.model.whiteness = Math.min(
Math.max(Math.floor(space.whiteness), 0),
100
);
this.model.blackness = Math.min(
Math.max(Math.floor(space.blackness), 0),
100
);
return this;
}
}