-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryb2rgb.js
355 lines (307 loc) · 10.9 KB
/
ryb2rgb.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**************************************************************************************************
* Color Space - A RGB to RYB converter.
*
* Copyright (C) 2013 Joshua Gentry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var RgbRyb = function() {
throw "Class not initialized yet."
};
/**************************************************************************************************
* This class deals with RGB and RYG color and converts between them.
*
* Member variables:
* pRgb - The color components in RGB format.
* pRyg - The color components in RYB format.
*/
function rgb_ryb() {
var E10001 = "E10001";
var E10002 = "E10002";
var E10003 = "E10003";
var E10004 = "E10004";
/**********************************************************************************************
* Construct a new object.
*
* @param iRed The initial red value.
* @param iGreen The initial green value.
* @param iBlue The initial blue value.
*/
RgbRyb = function(iRed, iGreen, iBlue) {
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
// Save the RGB.
this.setRgb(iRed, iGreen, iBlue);
}
/**********************************************************************************************
* Given a RGB color, calculate the RYB color. This code was taken from:
*
* @param iRed The current red value.
* @param iGreen The current green value.
* @param iBlue The current blue value.
*
* http://www.insanit.net/tag/rgb-to-ryb/
*
* Author: Arah J. Leonard
* Copyright 01AUG09
* Distributed under the LGPL - http://www.gnu.org/copyleft/lesser.html
* ALSO distributed under the The MIT License from the Open Source Initiative (OSI) -
* http://www.opensource.org/licenses/mit-license.php
* You may use EITHER of these licenses to work with / distribute this source code.
* Enjoy!
*/
RgbRyb.prototype.setRgb = function(iRed, iGreen, iBlue) {
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
// Save the RGB
this.pRgb = [iRed, iGreen, iBlue];
// Remove the white from the color
var iWhite = Math.min(iRed, iGreen, iBlue);
iRed -= iWhite;
iGreen -= iWhite;
iBlue -= iWhite;
var iMaxGreen = Math.max(iRed, iGreen, iBlue);
// Get the yellow out of the red+green
var iYellow = Math.min(iRed, iGreen);
iRed -= iYellow;
iGreen -= iYellow;
// If this unfortunate conversion combines blue and green, then cut each in half to
// preserve the value's maximum range.
if (iBlue > 0 && iGreen > 0) {
iBlue /= 2;
iGreen /= 2;
}
// Redistribute the remaining green.
iYellow += iGreen;
iBlue += iGreen;
// Normalize to values.
var iMaxYellow = Math.max(iRed, iYellow, iBlue);
if (iMaxYellow > 0) {
var iN = iMaxGreen / iMaxYellow;
iRed *= iN;
iYellow *= iN;
iBlue *= iN;
}
// Add the white back in.
iRed += iWhite;
iYellow += iWhite;
iBlue += iWhite;
this.pRyb = [Math.floor(iRed), Math.floor(iYellow), Math.floor(iBlue)];
}
/**********************************************************************************************
* Given a RYB color, calculate the RGB color. This code was taken from:
*
* @param iRed The current red value.
* @param iYellow The current yellow value.
* @param iBlue The current blue value.
*
* http://www.insanit.net/tag/rgb-to-ryb/
*
* Author: Arah J. Leonard
* Copyright 01AUG09
* Distributed under the LGPL - http://www.gnu.org/copyleft/lesser.html
* ALSO distributed under the The MIT License from the Open Source Initiative (OSI) -
* http://www.opensource.org/licenses/mit-license.php
* You may use EITHER of these licenses to work with / distribute this source code.
* Enjoy!
*/
RgbRyb.prototype.setRyb = function(iRed, iYellow, iBlue) {
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
// Save the RYB
this.pRyb = [iRed, iYellow, iBlue];
// Remove the whiteness from the color.
var iWhite = Math.min(iRed, iYellow, iBlue);
iRed -= iWhite;
iYellow -= iWhite;
iBlue -= iWhite;
var iMaxYellow = Math.max(iRed, iYellow, iBlue);
// Get the green out of the yellow and blue
var iGreen = Math.min(iYellow, iBlue);
iYellow -= iGreen;
iBlue -= iGreen;
if (iBlue > 0 && iGreen > 0) {
iBlue *= 2.0;
iGreen *= 2.0;
}
// Redistribute the remaining yellow.
iRed += iYellow;
iGreen += iYellow;
// Normalize to values.
var iMaxGreen = Math.max(iRed, iGreen, iBlue);
if (iMaxGreen > 0) {
var iN = iMaxYellow / iMaxGreen;
iRed *= iN;
iGreen *= iN;
iBlue *= iN;
}
// Add the white back in.
iRed += iWhite;
iGreen += iWhite;
iBlue += iWhite;
// Save the RGB
this.pRgb = [Math.floor(iRed), Math.floor(iGreen), Math.floor(iBlue)];
}
/**********************************************************************************************
* Returns the hex code for three digits in an array.
*
* @param aData An array of three integers to return the hex code for.
*
* @returns The hex code for the three numbers
*/
RgbRyb.prototype.getHexCode = function(aData) {
//-----------------------------------------------------------------------------------------
// Create the hex code for the array.
var szColor = "";
var szOne = aData[0].toString(16);
var szTwo = aData[1].toString(16);
var szThr = aData[2].toString(16);
if (szOne.length == 1) szColor += "0" + szOne;
else szColor += szOne;
if (szTwo.length == 1) szColor += "0" + szTwo;
else szColor += szTwo;
if (szThr.length == 1) szColor += "0" + szThr;
else szColor += szThr;
return (szColor);
}
/**********************************************************************************************
* Calculate the luminance of the color.
*
* @returns The luminance of the color in the range 0 to 255.
*/
RgbRyb.prototype.getLuminance = function() {
return (Math.floor(0.2126 * this.pRgb[0] + 0.7152 * this.pRgb[1] + 0.0722 * this.pRgb[2]));
}
/**********************************************************************************************
* Return the RGB color in #RRGGBB format.
*
* @returns The RGB color in #RRGGBB.
*/
RgbRyb.prototype.getRgbText = function() {
return ("#" + this.getHexCode(this.pRgb));
}
/**********************************************************************************************
* Return the RYB color in #RRYYBB format.
*
* @returns The RYB color in #RRYYBB.
*/
RgbRyb.prototype.getRybText = function() {
return ("#" + this.getHexCode(this.pRyb));
}
/**********************************************************************************************
* Returns the red component of the RGB value.
*
* @return The red color 0 - 255.
*/
RgbRyb.prototype.getRgbRed = function() {
return (this.pRgb[0]);
}
/**********************************************************************************************
* Returns the green component of the RGB value.
*
* @return The green color 0 - 255.
*/
RgbRyb.prototype.getRgbGreen = function() {
return (this.pRgb[1]);
}
/**********************************************************************************************
* Returns the blue component of the RGB value.
*
* @return The blue color 0 - 255.
*/
RgbRyb.prototype.getRgbBlue = function() {
return (this.pRgb[2]);
}
/**********************************************************************************************
* Returns the red component of the RYB value.
*
* @return The red color 0 - 255.
*/
RgbRyb.prototype.getRybRed = function() {
return (this.pRyb[0]);
}
/**********************************************************************************************
* Returns the yellow component of the RYB value.
*
* @return The yellow color 0 - 255.
*/
RgbRyb.prototype.getRybYellow = function() {
return (this.pRyb[1]);
}
/**********************************************************************************************
* Returns the blue component of the RYB value.
*
* @return The blue color 0 - 255.
*/
RgbRyb.prototype.getRybBlue = function() {
return (this.pRyb[2]);
}
this.init = function() {
// method body
}();
}
function hex2rgb(hex) {
if (hex[0] == "#") {
hex = hex.slice(1, hex.length);
}
let r = parseInt(hex.slice(0, 2), 16);
let g = parseInt(hex.slice(2, 4), 16);
let b = parseInt(hex.slice(4, 6), 16);
return "rgb("+r+", "+g+", "+b+")";
}
let MAX_RGB_SUM = 384;
function chs2ryb(c, h, s) {
c = c * MAX_RGB_SUM;
h = h * MAX_RGB_SUM;
s = s * MAX_RGB_SUM;
let max = Math.max(c, h, s);
if (max > 255) {
let ratio = 255/max;
c = c * ratio;
h = h * ratio;
s = s * ratio;
}
return [c, h, s];
}
// rgb_ryb();
// var color = new RgbRyb();
// color.setRyb(128, 128, 128);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(255, 128, 128);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(128, 255, 128);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(128, 128, 255);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 255, 0);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(127, 255, 0);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 255, 0);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 255, 127);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 255, 255);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 127, 255);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(0, 0, 255);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(127, 0, 255);
// console.log(hex2rgb(color.getRgbText()));
// color.setRyb(255, 0, 255);
// console.log(hex2rgb(color.getRgbText()));
// for (var i = 0; i < 12; i++) {
// console.log(i*(1/11));
// }