-
Notifications
You must be signed in to change notification settings - Fork 6
/
ArtColors.cpp
549 lines (462 loc) · 19.6 KB
/
ArtColors.cpp
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//ArtColors by Eric J. Jenislawski
//Version 0.2
//Subtractive Red-Yellow-Blue color mixing and palette selector
//Copyright 2020, Eric J. Jenislawski. Licensed under GNU General Public License version 3 or later.
//The extremely useful RayLib and RayGUI libraries are by Ramon Santamaria (raysan5) https://github.com/raysan5/
#include "raylib.h"
#include "raymath.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include <iostream>
#include <cstdio> //for snprintf
#include <cmath> //for fabs
#include <vector> // for C++ vector
#include <fstream> //for saving palettes
#include <string> // for to_string() for saving palettes
#include <algorithm>
#include "RYB.h"
using namespace std;
//Full color from 300 degrees to 60 degrees. No color on [120,240]. Smoothstep transition in between.
float step2(float deg) {
float out=0.0;
float sc=0.0;
while (deg<0.0) { deg+=360.0;}
while (deg>360.0) { deg-=360.0;}
if (deg<=60.0) {
out=1.0;
}
else if ( (deg>60.0)&&(deg<=120.0) ) {
sc=(deg-60.0)/60.0;
out=1.0-2.0*sc/sqrt(1.0+3.0*sc*sc);
}
else if ( (deg>120.0) && (deg<=240.0) ) {
out=0.0;
}
else if ( (deg>240.0) && (deg<=300.0) ) {
sc=(deg-240.0)/60.0;
out=2.0*sc/sqrt(1.0+3.0*sc*sc);
}
else if ( (deg>300.0) && (deg<=360.0) ) {
out=1.0;
}
return out;
}
Color map2(float deg) {
Vector3 out;
Color output;
//Function-based color spread around the wheel
out.x=255*step2(deg);
out.y=255*step2(deg-120);
out.z=255*step2(deg-240);
output=Xform_RYB2RGB(out.x,out.y,out.z);
return output;
}
// MAIN
int main()
{
//Initialize Raylib
InitWindow(1500, 900, "ArtColors: RYB mixer and palette selector");
SetWindowPosition(400,50);
Camera2D camera = { 0 };
camera.target = (Vector2){ 0, 0 };
camera.offset = (Vector2){ 0, 0 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(30);
Color col=BLUE;
float degrees=0.0;
float brightness=0.0;
float sat=0.0;
float myred=0.0,myyellow=0.0,myblue=0.0;
float myred2=0.0,myyellow2=0.0,myblue2=0.0;
char textout[20];
Color BackgroundColor = BLACK;
vector<float> PaletteDegrees;
Vector2 CircleCenter={300,250};
Vector2 MousePosition;
Color mix_a, mix_b,pixcol_a,pixcol_b;
bool mix_a_hook=false,mix_b_hook=false;
bool HelpButtonState=false;
int HelpBoxState = 0;
bool TextExportButtonState=false;
bool TextExportButtonState2=false;
bool TextExportButtonState3=false;
bool TextExportButtonState4=false;
bool TextExportButtonState5=false;
bool ScreenShotButtonState=false;
bool DrawInverseColors=false;
int PaletteSelectorResult = 0;
int CanvasToggleGroup = 0;
bool ColorPickerActive=false;
int ColorblindMode=0;
bool RenderForColorDeficiency=false;
while (!WindowShouldClose()){
//Update
//TODO:
//Get something better for a color picker for any hue on the screen.
//Keep screen shot as method of export.
if(ColorPickerActive) {
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
MousePosition=GetMousePosition();
cout<<"Mouse X:"<<MousePosition.x<<" Y="<<MousePosition.y<<endl;
Image screenshot=GetScreenData();
Color* sscols=new Color[1500*900];
sscols=GetImageData(screenshot);
pixcol_a=sscols[1500*((int)MousePosition.y)+(int)MousePosition.x];
printf("R=%i, G=%i, B=%i \n",pixcol_a.r,pixcol_a.g,pixcol_a.b);
UnloadImage(screenshot);
delete[] sscols;
mix_a_hook=true;
ColorPickerActive=false;
}
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
MousePosition=GetMousePosition();
cout<<"Mouse X:"<<MousePosition.x<<" Y="<<MousePosition.y<<endl;
Image screenshot=GetScreenData();
Color* sscols=new Color[1500*900];
sscols=GetImageData(screenshot);
pixcol_b=sscols[1500*((int)MousePosition.y)+(int)MousePosition.x];
printf("R=%i, G=%i, B=%i \n",pixcol_b.r,pixcol_b.g,pixcol_b.b);
UnloadImage(screenshot);
delete[] sscols;
mix_b_hook=true;
ColorPickerActive=false;
}
}
//Draw
BeginDrawing();
ClearBackground(BackgroundColor);
BeginMode2D(camera);
DrawText("Canvas Color",10,30,16,GRAY);
CanvasToggleGroup = GuiToggleGroup((Rectangle){ 10, 50, 50, 25 }, "BLACK;WHITE", CanvasToggleGroup);
switch (CanvasToggleGroup)
{
case 0:
BackgroundColor=BLACK;
break;
case 1:
BackgroundColor=WHITE;
break;
default:
BackgroundColor=BLACK;
}
for (int i=0;i<360;i+=2) {
col=map2((float)i);
col=Saturate(col,sat);
col=Brighten(col,brightness);
DrawCircleSector(CircleCenter,200,i,i+5,6,col);
//DrawCircleSector({1000,350},200,i,i+5,6,(Color){RYBCol.x,RYBCol.y,RYBCol.z,255});
}
degrees=GuiSlider({150,500,360,20},NULL,NULL,degrees,0.0,359.0);
snprintf(textout,sizeof textout,"%f",degrees);
DrawText(textout,520,500,20,GRAY);
DrawText("Hue:",100,500,20,GRAY);
brightness=GuiSlider({150,550,360,20},NULL,NULL,brightness,-1.0,1.0);
if (fabs(brightness)<0.05) {brightness=0.0;}
snprintf(textout,sizeof textout,"%f",brightness);
DrawText(textout,520,550,20,GRAY);
DrawText("Tint/Shade:",20,550,20,GRAY);
sat=GuiSlider({150,600,360,20},NULL,NULL,sat,-1.0,1.0);
if (fabs(sat)<0.05) {sat=0.0;}
snprintf(textout,sizeof textout,"%f",sat);
DrawText(textout,520,600,20,GRAY);
DrawText("Saturate:",40,600,20,GRAY);
ColorPickerActive=GuiToggle({150,640,80,20},"Pick Color",ColorPickerActive);
DrawText("Left click on Color A, right click on B",250,640,16,GRAY);
//Color Palette Selection
DrawText("Palette Type",550,30,20,GRAY);
PaletteSelectorResult = GuiComboBox((Rectangle){ 550, 50, 150, 30 }, "Triadic;SplitComp;SqTetrad;RectTetrad;Analogous;Compl", PaletteSelectorResult);
PaletteDegrees.clear();
switch (PaletteSelectorResult)
{
case 0:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+120.0);
PaletteDegrees.push_back(degrees+240.0);
break;
case 1:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+150.0);
PaletteDegrees.push_back(degrees+210.0);
break;
case 2:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+90.0);
PaletteDegrees.push_back(degrees+180.0);
PaletteDegrees.push_back(degrees+270.0);
break;
case 3:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+120.0);
PaletteDegrees.push_back(degrees+180.0);
PaletteDegrees.push_back(degrees+300.0);
break;
case 4:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+15.0);
PaletteDegrees.push_back(degrees+30.0);
PaletteDegrees.push_back(degrees+45.0);
break;
case 5:
PaletteDegrees.push_back(degrees);
PaletteDegrees.push_back(degrees+180.0);
break;
}
//Draw Indicators Around Color Wheel
for (unsigned int s=0;s<PaletteDegrees.size();s++) {
float deg=PaletteDegrees[s];
col=map2(deg);
col=Saturate(col,sat);
col=Brighten(col,brightness);
DrawRectangle(CircleCenter.x+220*cos(DEG2RAD*(-deg+90)),CircleCenter.y+220*sin(DEG2RAD*(-deg+90)),10,10,col);
DrawRectangle(550,200+30*s,40,20,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,600,200+30*s,20,GRAY);
}
/*
//Alternative Color Picker
float picker=GuiSlider({950,50,510,20},NULL,NULL,picker,0,255);
for (int i=0;i<255;i++) {
for (int j=0;j<255;j++) {
col=Xform_RYB2RGB((int)picker,i,j);
col=Saturate(col,sat);
col=Brighten(col,brightness);
DrawRectangle(1000+i,100+j,5,5,col);
}
}
*/
//Palettizer
GuiGroupBox((Rectangle){ 800,150,615,215 }, "Swatches");
for (unsigned int s=0;s<PaletteDegrees.size();s++) {
col=map2(PaletteDegrees[s]);
Color PCol;
int sep=60/PaletteDegrees.size();
for (float i=-0.8;i<1.0;i+=0.2) {
for (float j=-0.8;j<=0.8;j+=0.2) {
PCol=Saturate(col,i);
PCol=Brighten(PCol,j);
if (RenderForColorDeficiency) {
PCol=ColorBlindTransform(PCol,ColorblindMode);
}
DrawRectangle(1050+i*300+sep*s,250+j*100,15,15,PCol);
}
}
}
/*
// RYB-RGB function and inverse test pattern
//A round-trip conversion from RGB to RYB to RGB. Helps show where trilinear interpretation values can be improved
//Uncomment this section to draw a color bar beneath the save buttons and above the color swatches
for (float i=0.0;i<360;i+=10.0) {
col=map2(i);
DrawRectangle(700+2*i,90,20,20,col);
col=Xform_RGB2RYB(col.r,col.g,col.b);
col=Xform_RYB2RGB(col.r,col.g,col.b);
col=Saturate(col,0.5);
DrawRectangle(700+2*i,110,20,20,col);
}
*/
//Custom blender
DrawText("Custom Color Blender",100,750,20,GRAY);
myred=GuiSlider({100,780,255,20},"Red",NULL,myred,0.0,255.0);
col=Xform_RYB2RGB(myred,0,0);
DrawRectangle(360,780,20,20,col);
myyellow=GuiSlider({100,810,255,20},"Yellow",NULL,myyellow,0.0,255.0);
col=Xform_RYB2RGB(0,myyellow,0);
DrawRectangle(360,810,20,20,col);
myblue=GuiSlider({100,840,255,20},"Blue",NULL,myblue,0.0,255.0);
col=Xform_RYB2RGB(0,0,myblue);
DrawRectangle(360,840,20,20,col);
col=Xform_RYB2RGB(myred,myyellow,myblue);
DrawRectangle(400,780,100,100,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,520,780,20,GRAY);
// mix_a=col;
if (mix_a_hook==true) {
mix_a=pixcol_a;
// mix_a_hook=false;
}
else {
mix_a=col;
}
myred2=GuiSlider({700,780,255,20},"Red",NULL,myred2,0.0,255.0);
col=Xform_RYB2RGB(myred2,0,0);
DrawRectangle(960,780,20,20,col);
myyellow2=GuiSlider({700,810,255,20},"Yellow",NULL,myyellow2,0.0,255.0);
col=Xform_RYB2RGB(0,myyellow2,0);
DrawRectangle(960,810,20,20,col);
myblue2=GuiSlider({700,840,255,20},"Blue",NULL,myblue2,0.0,255.0);
col=Xform_RYB2RGB(0,0,myblue2);
DrawRectangle(960,840,20,20,col);
col=Xform_RYB2RGB(myred2,myyellow2,myblue2);
DrawRectangle(1000,780,100,100,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,1120,780,20,GRAY);
// mix_b=col;
if (mix_b_hook==true) {
mix_b=pixcol_b;
// mix_b_hook=false;
}
else {
mix_b=col;
}
DrawText("Additive",700,410,16,GRAY);
DrawText("Quadratic:",700,430,16,GRAY);
DrawText("Additive",700,480,16,GRAY);
DrawText("Linear:",700,500,16,GRAY);
DrawText("Subtractive:",700,560,16,GRAY);
DrawInverseColors=GuiToggle((Rectangle){700,620,10,10}," ",DrawInverseColors);
DrawText("Inverse:",700,640,16,GRAY);
for (float i=0.0;i<1.1;i+=0.1) {
col=ColorMix(mix_a,mix_b,i);
DrawRectangle(800+(600*i),400,60,50,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,800+(600*i),450,10,GRAY);
col=ColorMixLin(mix_a,mix_b,i);
DrawRectangle(800+(600*i),470,60,50,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,800+(600*i),520,10,GRAY);
col=ColorMixSub(mix_a,mix_b,i);
DrawRectangle(800+(600*i),540,60,50,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,800+(600*i),590,10,GRAY);
if (DrawInverseColors) {
col=ColorInv(col);
DrawRectangle(800+(600*i),620,60,30,col);
snprintf(textout,sizeof textout,"%i %i %i",col.r,col.g,col.b);
DrawText(textout,800+(600*i),660,10,GRAY);
}
}
//Export and Help Buttons
TextExportButtonState=GuiButton((Rectangle){800,50,130,35},"Save Swatches to .pal");
if (TextExportButtonState) {
ofstream PaletteFile;
PaletteFile.open("ArtColor-Swatches.pal");
if (PaletteFile.is_open()) {
PaletteFile<<"JASC-PAL\r\n";
PaletteFile<<"0100\r\n";
PaletteFile<<to_string(PaletteDegrees.size()+72*PaletteDegrees.size())<<"\r\n";
for (unsigned int s=0;s<PaletteDegrees.size();s++) { //This part should match Palettizer loops. TODO: make a subroutine for both.
col=map2(PaletteDegrees[s]);
Color PCol;
for (float i=-0.8;i<1.0;i+=0.2) {
for (float j=-0.8;j<=0.8;j+=0.2) {
PCol=Saturate(col,i);
PCol=Brighten(PCol,j);
PaletteFile<<to_string(PCol.r)<<" "<<to_string(PCol.g)<<" "<<to_string(PCol.b)<<"\r\n";
}
}
}
PaletteFile.close();
cout<<"Palette file saved."<<endl;
}
else {
cout<<"File creation failed."<<endl;
}
}
DrawText("Save Color Bars:",950,30,20,GRAY);
TextExportButtonState2=GuiButton((Rectangle){950,50,50,35},"Quad+");
if (TextExportButtonState2) {
ofstream PaletteFile;
PaletteFile.open("ArtColor-AddQuadBars.pal");
if (PaletteFile.is_open()) {
PaletteFile<<"JASC-PAL\r\n";
PaletteFile<<"0100\r\n";
PaletteFile<<to_string(11)<<"\r\n";
for (float i=0.0;i<1.1;i+=0.1) {
col=ColorMix(mix_a,mix_b,i);
PaletteFile<<to_string(col.r)<<" "<<to_string(col.g)<<" "<<to_string(col.b)<<"\r\n";
}
PaletteFile.close();
cout<<"Additive Quadratic bars palette saved."<<endl;
}
else {
cout<<"File creation failed."<<endl;
}
}
TextExportButtonState3=GuiButton((Rectangle){1020,50,50,35},"Lin+");
if (TextExportButtonState3) {
ofstream PaletteFile;
PaletteFile.open("ArtColor-AddLinBars.pal");
if (PaletteFile.is_open()) {
PaletteFile<<"JASC-PAL\r\n";
PaletteFile<<"0100\r\n";
PaletteFile<<to_string(11)<<"\r\n";
for (float i=0.0;i<1.1;i+=0.1) {
col=ColorMixLin(mix_a,mix_b,i);
PaletteFile<<to_string(col.r)<<" "<<to_string(col.g)<<" "<<to_string(col.b)<<"\r\n";
}
PaletteFile.close();
cout<<"Additive Linear bars palette saved."<<endl;
}
else {
cout<<"File creation failed."<<endl;
}
}
TextExportButtonState4=GuiButton((Rectangle){1090,50,50,35},"Subtract");
if (TextExportButtonState4) {
ofstream PaletteFile;
PaletteFile.open("ArtColor-SubBars.pal");
if (PaletteFile.is_open()) {
PaletteFile<<"JASC-PAL\r\n";
PaletteFile<<"0100\r\n";
PaletteFile<<to_string(11)<<"\r\n";
for (float i=0.0;i<1.1;i+=0.1) {
col=ColorMixSub(mix_a,mix_b,i);
PaletteFile<<to_string(col.r)<<" "<<to_string(col.g)<<" "<<to_string(col.b)<<"\r\n";
}
PaletteFile.close();
cout<<"Subtractive mixing bars palette saved."<<endl;
}
else {
cout<<"File creation failed."<<endl;
}
}
if (DrawInverseColors) {
TextExportButtonState5=GuiButton((Rectangle){1160,50,50,35},"Inv. Sub");
if (TextExportButtonState5) {
ofstream PaletteFile;
PaletteFile.open("ArtColor-Inverse.pal");
if (PaletteFile.is_open()) {
PaletteFile<<"JASC-PAL\r\n";
PaletteFile<<"0100\r\n";
PaletteFile<<to_string(11)<<"\r\n";
for (float i=0.0;i<1.1;i+=0.1) {
col=ColorMixSub(mix_a,mix_b,i);
col=ColorInv(col);
PaletteFile<<to_string(col.r)<<" "<<to_string(col.g)<<" "<<to_string(col.b)<<"\r\n";
}
PaletteFile.close();
cout<<"Inverse bars palette saved."<<endl;
}
else {
cout<<"File creation failed."<<endl;
}
}
}
DrawText("Colorblind mode:",1240,780,20,GRAY);
ColorblindMode = GuiComboBox((Rectangle){ 1240, 820, 150, 35 }, "Normal;Protanopia;Deuteranopia;Tritanopia;Achromatopsia", ColorblindMode);
if (ColorblindMode==0) RenderForColorDeficiency=false; else RenderForColorDeficiency=true;
ScreenShotButtonState=GuiButton((Rectangle){1300,50,70,35},"Screen Shot");
HelpButtonState=GuiToggle((Rectangle){1380,50,50,35},"Help",HelpButtonState);
if (HelpButtonState) {
string helpmessage="Choose a palette by moving the Hue / Brightness / Saturation sliders and selecting your palette type by cycling clicking the Palette Type button.\n";
helpmessage+="This will update the color swatches on the right which span a range of tints and saturations.\n";
helpmessage+="You can blend two colors by selecting them using the Pick Color button. Click the button then click anywhere in the program window to sample a color, i.e., eyedropper mode\n";
helpmessage+="Left click selects the first color for blending and right click selects the second color. The mixing bars show different ways of mixing the two colors. Subtractive uses the ArtColors formula.\n";
helpmessage+="You can take the colors from the color wheel or use the RYB blending areas below to specify your own colors then select them to mix them.\n";
helpmessage+="The save buttons above the swatches will export palette values for the swatches or for the mixing bars and inverse color bar respectively.\n";
helpmessage+="The Colorblind Mode selector (lower right) replicates how the palette in the swatches would appear to people with various color vision deficiencies.\n";
helpmessage+="You can use it to check, for example, whether your choice of palette colors are visually distinct to the colorblind.\n";
helpmessage+="All triplets of numbers are RGB values. Pressing ESC quits the program. \n";
HelpBoxState=GuiMessageBox((Rectangle){200,400,1000,400},"Help Info",helpmessage.c_str(),"OK");
if (HelpBoxState==0||HelpBoxState==1) HelpButtonState=false;
}
EndMode2D();
// DrawFPS(10,10);
EndDrawing();
if (ScreenShotButtonState) {
TakeScreenshot("ArtColors-ScreenShot.png");
}
}
return 0;
}