1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | /* |
19 | Changes: |
20 | - v4.0.0: |
21 | - created from v3.6.0 |
22 | Doc reviewed 202208 |
23 | */ |
24 | |
25 | import { ZERO, TWO, THREE, HEXADECIMAL } from '../../main/Constants.js'; |
26 | |
27 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
28 | /** |
29 | A simple container for a color, mainly used to convert from css format to numbers and vice-versa |
30 | */ |
31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
32 | |
33 | class Color { |
34 | |
35 | /** |
36 | The red value of the color |
37 | @type {Number} |
38 | */ |
39 | |
40 | #red; |
41 | |
42 | /** |
43 | The green value of the color |
44 | @type {Number} |
45 | */ |
46 | |
47 | #green; |
48 | |
49 | /** |
50 | The blue value of the color |
51 | @type {Number} |
52 | */ |
53 | |
54 | #blue; |
55 | |
56 | /** |
57 | The constructor |
58 | @param {?number} red The red value of the color. Must be between 0 and 255. If null set to 255 |
59 | @param {?number} green The green value of the color. Must be between 0 and 255. If null set to 255 |
60 | @param {?number} blue The blue value of the color. Must be between 0 and 255. If null set to 255 |
61 | */ |
62 | |
63 | constructor ( red, green, blue ) { |
64 | Object.freeze ( this ); |
65 | this.#red = |
66 | 'number' === typeof red && Color.MAX_COLOR >= red && Color.MIN_COLOR <= red |
67 | ? |
68 | red |
69 | : |
70 | Color.MAX_COLOR; |
71 | this.#green = |
72 | 'number' === typeof green && Color.MAX_COLOR >= green && Color.MIN_COLOR <= green |
73 | ? |
74 | green |
75 | : |
76 | Color.MAX_COLOR; |
77 | this.#blue = |
78 | 'number' === typeof blue && Color.MAX_COLOR >= blue && Color.MIN_COLOR <= blue |
79 | ? |
80 | blue |
81 | : |
82 | Color.MAX_COLOR; |
83 | } |
84 | |
85 | /** |
86 | The smallest acceptable value fo a color |
87 | @type {Number} |
88 | */ |
89 | |
90 | // eslint-disable-next-line no-magic-numbers |
91 | static get MIN_COLOR ( ) { return 0; } |
92 | |
93 | /** |
94 | The bigest acceptable value fo a color |
95 | @type {Number} |
96 | */ |
97 | |
98 | // eslint-disable-next-line no-magic-numbers |
99 | static get MAX_COLOR ( ) { return 255; } |
100 | |
101 | /** |
102 | A static method that build a Color object from a css color string |
103 | @param {String} cssColor The css color string |
104 | @return {Color} A new color object set to the color in the css string |
105 | */ |
106 | |
107 | static fromCss ( cssColor ) { |
108 | let color = new Color ( ); |
109 | color.cssColor = cssColor; |
110 | return color; |
111 | } |
112 | |
113 | /** |
114 | The red value of the color |
115 | @type {Number} |
116 | */ |
117 | |
118 | get red ( ) { return this.#red; } |
119 | |
120 | set red ( red ) { |
121 | if ( 'number' === typeof red && Color.MAX_COLOR >= red && Color.MIN_COLOR <= red ) { |
122 | this.#red = red; |
123 | } |
124 | } |
125 | |
126 | /** |
127 | The green value of the color |
128 | @type {Number} |
129 | */ |
130 | |
131 | get green ( ) { return this.#green; } |
132 | |
133 | set green ( green ) { |
134 | if ( 'number' === typeof green && Color.MAX_COLOR >= green && Color.MIN_COLOR <= green ) { |
135 | this.#green = green; |
136 | } |
137 | } |
138 | |
139 | /** |
140 | The blue value of the color |
141 | @type {Number} |
142 | */ |
143 | |
144 | get blue ( ) { return this.#blue; } |
145 | |
146 | set blue ( blue ) { |
147 | if ( 'number' === typeof blue && Color.MAX_COLOR >= blue && Color.MIN_COLOR <= blue ) { |
148 | this.#blue = blue; |
149 | } |
150 | } |
151 | |
152 | /** |
153 | The color in the css HEX format '#RRGGBB' or 'rgb( )' |
154 | @type {String} |
155 | */ |
156 | |
157 | get cssColor ( ) { |
158 | return '\u0023' + |
159 | this.#red.toString ( HEXADECIMAL ).padStart ( TWO, '0' ) + |
160 | this.#green.toString ( HEXADECIMAL ).padStart ( TWO, '0' ) + |
161 | this.#blue.toString ( HEXADECIMAL ).padStart ( TWO, '0' ); |
162 | } |
163 | |
164 | set cssColor ( cssColor ) { |
165 | if ( '\u0023' === cssColor [ ZERO ] ) { |
166 | // eslint-disable-next-line no-magic-numbers |
167 | this.#red = Number.parseInt ( cssColor.substring ( 1, 3 ), HEXADECIMAL ); |
168 | // eslint-disable-next-line no-magic-numbers |
169 | this.#green = Number.parseInt ( cssColor.substring ( 3, 5 ), HEXADECIMAL ); |
170 | // eslint-disable-next-line no-magic-numbers |
171 | this.#blue = Number.parseInt ( cssColor.substring ( 5, 7 ), HEXADECIMAL ); |
172 | } |
173 | else if ( 'rgb' === cssColor.substring ( ZERO, THREE ) ) { |
174 | [ this.#red, this.#green, this.#blue ] = |
175 | Array.from ( cssColor.match ( /[0-9]{1,3}/g ), value => Number.parseInt ( value ) ); |
176 | } |
177 | } |
178 | } |
179 | |
180 | export default Color; |
181 | |
182 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
183 |