-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPatch.pde
More file actions
548 lines (508 loc) · 19.2 KB
/
ColorPatch.pde
File metadata and controls
548 lines (508 loc) · 19.2 KB
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
// Constants - some are fuzzy, picked based upon by my experimentation
final int contourColorThreshold = 60; // RGB diff to identify contour line
// flag to show contour lines
boolean bShowContour = true;
// Class for an individual Color Patch
class ColorPatch
{
public int id; // ID of the patch: serial number starting from 0
public ArrayList<PVector> points; // coordinates of all the points
public ArrayList<PVector> contourPoints; // coordinates of contour
public float xMin, xMax, yMin, yMax; // dimension of the contour in rectangle
public float masterGray; // master gray level. 0 if not specified
public float masterHue; // master hue degree. negative if not specified
public float masterChroma; // master chroma - always used together with masterHue
public float masterTension; // master tension. 0 if not specified
public String masterTransition; // master transition spec
public MunsellColor mColor; // painted Munsell color of the patch - always one from the quantized map (mToRGB)
public color rgbColor; // RGB color of mColor for display
public float gGap; // Color Gap in terms of gray level against the centroid
public float cGap; // Color Gap in terms of color against the centroid
// transient - populated on-demand for performance. no need to serialize
public PVector coord; // Color Coordinate
public boolean transParsed; // flag if transition parameters have been parsed.
public int transRefIdx; // transition reference patch index
public ArrayList<int[]> transRules; // transition rules. [0]: type, [1]: delta
public color[] texture; // texture cache of the patch
public int textureType; // texture info used in the cache
public int textureAxis;
public int textureGap;
public int textureBG;
public boolean contourPtsSorted; // flag if contour points are sorted
// default constructor
public ColorPatch() {}
// Constructor to build a patch from pixels
public ColorPatch(int id, PImage img, ArrayList<PVector> pts, ArrayList<PVector> cpts)
{
this.id = id;
points = pts;
contourPoints = cpts;
// Set the dimension
xMin = Integer.MAX_VALUE;
xMax = Integer.MIN_VALUE;
yMin = Integer.MAX_VALUE;
yMax = Integer.MIN_VALUE;
for (PVector p : points) {
xMin = min(xMin, p.x);
xMax = max(xMax, p.x);
yMin = min(yMin, p.y);
yMax = max(yMax, p.y);
}
// Initialize master color/tension
masterGray = 0;
masterHue = -1;
masterChroma = -1;
masterTension = 0;
masterTransition = "";
// Calculate average color
float rSum = 0;
float gSum = 0;
float bSum = 0;
float incr = max(1, points.size() / 100); // use only 100 points at max
int np = 0;
for (float i = 0; i < points.size(); i += incr) {
PVector p = points.get((int)i);
color c = img.get((int)p.x, (int)p.y);
rSum += red(c);
gSum += green(c);
bSum += blue(c);
np++;
}
setColor(colorTable.rgbToMunsell(color(rSum / np, gSum / np, bSum / np)));
// Make sure the value of the color is within [2, 18] for painting purpose.
// Value outside this range won't be honored when selecting a color in painting.
if (mColor.value < 2 || mColor.value > 18) {
mColor.value = min(18, mColor.value);
mColor.value = max(2, mColor.value);
setColor(mColor);
}
gGap = cGap = 0; // set by updateGap() later
transParsed = false;
texture = null;
contourPtsSorted = false;
}
// Copy constructor for a shallow copy to experiment all while keeping the shape and location
public ColorPatch(ColorPatch p)
{
id = p.id;
points = p.points; // shallow copy
contourPoints = p.contourPoints;
xMin = p.xMin; xMax = p.xMax; yMin = p.yMin; yMax = p.yMax;
masterGray = p.masterGray;
masterHue = p.masterHue;
masterChroma = p.masterChroma;
masterTension = p.masterTension;
masterTransition = String.valueOf(p.masterTransition);
mColor = new MunsellColor(p.mColor);
rgbColor = p.rgbColor;
gGap = p.gGap;
cGap = p.cGap;
transParsed = false;
texture = p.texture;
textureType = p.textureType;
textureAxis = p.textureAxis;
textureGap = p.textureGap;
textureBG = p.textureBG;
contourPtsSorted = p.contourPtsSorted;
}
// Sort contour points in the order of proximity so it forms
// continous lines as possible, although it may have
// split or disjoint lines
public void sortContourPts()
{
if (contourPtsSorted)
return;
for (int i = 0; i < contourPoints.size(); i++) {
PVector last = contourPoints.get(i); // last connected or new start
for (int j = i + 1; j < contourPoints.size(); j++) {
PVector v = contourPoints.get(j);
if (abs(last.x - v.x) <= 1.0 && abs(last.y - v.y) <= 1.0) {
// a neighbor: pick it up and continue
PVector t1 = contourPoints.get(i + 1);
PVector t2 = contourPoints.get(j);
float tx = t1.x, ty = t1.y;
t1.x = t2.x; t1.y = t2.y;
t2.x = tx; t2.y = ty;
break;
}
}
}
contourPtsSorted = true;
}
// Add another patch into this one.
// Keep all the color and master info unchanged, but
// just to add the area of another patch.
// NOTE: the contourPoints will be simply added even if
// added contour lines may be no longer contour after added.
public void add(ColorPatch p)
{
assert mColor.isEqual(p.mColor) : "Cannot add a patch of different color into another.";
points.addAll(p.points);
contourPoints.addAll(p.contourPoints);
// Adjust the dimension
xMin = min(xMin, p.xMin);
xMax = max(xMax, p.xMax);
yMin = min(yMin, p.yMin);
yMax = max(yMax, p.yMax);
// Invalidate texture cache
texture = null;
contourPtsSorted = false;
}
// Serialize to save
public void Serialize(ObjectOutputStream oos) throws IOException
{
oos.writeInt(id);
oos.writeObject(points);
oos.writeObject(contourPoints);
oos.writeFloat(xMin); oos.writeFloat(xMax); oos.writeFloat(yMin); oos.writeFloat(yMax);
oos.writeFloat(masterGray);
oos.writeFloat(masterHue);
oos.writeFloat(masterChroma);
oos.writeFloat(masterTension);
oos.writeObject(masterTransition);
mColor.Serialize(oos);
oos.writeInt(rgbColor);
oos.writeFloat(gGap);
oos.writeFloat(cGap);
}
// Deserialize to load
public void Deserialize(ObjectInputStream ois) throws IOException, ClassNotFoundException
{
id = ois.readInt();
points = (ArrayList<PVector>)ois.readObject();
contourPoints = (ArrayList<PVector>)ois.readObject();
xMin = ois.readFloat(); xMax = ois.readFloat(); yMin = ois.readFloat(); yMax = ois.readFloat();
masterGray = ois.readFloat();
masterHue = ois.readFloat();
masterChroma = ois.readFloat();
masterTension = ois.readFloat();
masterTransition = (String)ois.readObject();
mColor = new MunsellColor(); mColor.Deserialize(ois);
rgbColor = ois.readInt();
gGap = ois.readFloat();
cGap = ois.readFloat();
transParsed = false;
texture = null;
contourPtsSorted = false;
}
// Set the color from a Munsell color. Return if changed.
public boolean setColor(MunsellColor mc)
{
// set RGB from the quantized mapping table to make sure
// the RGB is always the one driven from the mapping table.
color rgbColorNew = colorTable.munsellToRGB(mc);
// Now, we set quantized Munsell color for the RGB
MunsellColor mColorNew = new MunsellColor(colorTable.rgbToMunsell(rgbColorNew));
boolean bChanged = false;
if (rgbColor != rgbColorNew || !mColor.isEqual(mColorNew)) {
rgbColor = rgbColorNew;
mColor = mColorNew;
texture = null; // invalidate texture cache
bChanged = true;
}
return bChanged;
}
// Set master gray from a Munsell color. Return true if changed
public boolean setMasterGray(MunsellColor mc)
{
boolean bChanged = false;
if (masterGray != mc.value) {
masterGray = mc.value;
bChanged = true;
}
return bChanged;
}
// Set master hue and chroma from a Munsell color. Return true if changed
public boolean setMasterHue(MunsellColor mc)
{
float masterHueNew = mc.isGray() ? -1 : mc.hueDegree;
float masterChromaNew = mc.isGray() ? -1 : mc.chroma;
boolean bChanged = false;
if (masterHue != masterHueNew || masterChroma != masterChromaNew) {
masterHue = masterHueNew;
masterChroma = masterChromaNew;
bChanged = true;
}
return bChanged;
}
// Set master tension. Return true if changed
public boolean setMasterTension(float t)
{
boolean bChanged = false;
if (masterTension != t) {
masterTension = t;
bChanged = true;
}
return bChanged;
}
// Set master transition. Return true if changed
public boolean setMasterTransition(String str)
{
String masterTransitionNew = trim(String.valueOf(str)).toUpperCase();
boolean bChanged = false;
if (!masterTransition.equals(masterTransitionNew)) {
masterTransition = masterTransitionNew;
bChanged = true;
}
return bChanged;
}
// Parse master transition string into cache with ref patch idx and rules.
// A non-empty transition string has the following format:
// "<ref>{/rule}", where 'ref' indicates referenced patch id, and
// each rule 'rule', seperated by '/', indicates movement of hue/chroma
// relative to the hue/chroma of reference, as follows:
// "H<delta>" - Move hue by delta. Delta is a directional unit of 40 hue sectors.
// "C<delta>" - Move chroma by delta.
// "V<delta>" - Move value by delta.
// "A<delta>" - Move by delta distance in any direction. This rule can appear
// at the end; no rule can be added after this.
// "O" - Move to the opposite crossing the center of color sphere.
public void parseTransParams(Painting owner)
{
if (transParsed)
return; // done already
transRefIdx = -1;
transRules = new ArrayList<int[]> ();
if (masterTransition.isEmpty())
return;
String[] data = masterTransition.split("/");
int i = 0;
try {
int refId = Integer.parseInt(data[i++]); // reference id
transRefIdx = owner.findPatchIdx(refId);
while (i < data.length) {
int[] r = new int[2];
r[0] = data[i].charAt(0);
if (r[0] != 'H' && r[0] != 'C' && r[0] != 'V' && r[0] != 'A' && r[0] != 'O')
break; // invalid rule
if (r[0] != 'O')
r[1] = Integer.parseInt(data[i].substring(1));
transRules.add(r);
i++;
if (r[0] == 'A')
break; // no rules can be added after 'A'
}
} catch (Exception e) { }
if (i < data.length) { // prematured break
transRefIdx = -1;
transRules = new ArrayList<int[]> ();
}
transParsed = true;
}
// Get the size of patch area
public float getAreaSize()
{
return points.size();
}
// Scale all the pixel locations within the patch
public void scale(float xScale, float yScale)
{
// Scale the points. This effectively scale contour as well
// because contour points are just references to regular area points.
for (PVector p : points) {
p.x *= xScale;
p.y *= yScale;
}
xMin *= xScale;
xMax *= xScale;
yMin *= yScale;
yMax *= yScale;
}
// Create string for the patch statistics
public String getStatString()
{
return "Patch ID: " + id +
"\nArea: " + String.format("%,d", (int)getAreaSize()) +
"\nGray Gap: " + String.format("%.1f", gGap) +
"\nColor Gap: " + String.format("%.1f", cGap);
}
// See if the patch contains the point
boolean contains(int x, int y)
{
if (x < xMin || x > xMax || y < yMin || y > yMax)
return false;
for (PVector p : points) {
if ((int)p.x == x && (int)p.y == y)
return true;
}
return false;
}
// Draw the color patch to the image with local color
public void draw(PImage img, int kind, Painting owner)
{
switch (kind) {
case areaViewColors: // Draw local color of the patches
drawPoints(img, rgbColor);
break;
case areaViewMGray: // Draw master gray-level of the patches
drawPoints(img, colorTable.munsellToRGB(new MunsellColor(0, 0, masterGray)));
break;
case areaViewMHue: // Draw master hue & chroma of the patches
{
float ch = (masterHue < 0) ? 0 : masterChroma;
float x = ch * cos(radians(masterHue));
float y = ch * sin(radians(masterHue));
int[] vd = {0, -2, +2, -4, +4, -6, +6};
for (int i = 0; i < vd.length; i++) { // pick a value near 12 for the hue/chroma
MunsellColor mc = new MunsellColor(x, y, 12 + vd[i]);
if (colorTable.isMunsellKeyInMap(mc)) {
drawPoints(img, colorTable.munsellToRGB(mc));
break;
}
}
break;
}
case areaViewTension: // Draw tension of the local color
case areaViewMTension: // Draw master tension
{
// Color tension value ranges from [0, 80] within Munsell sphere.
// However, practically, most of tension values are within 40 (the radius).
// I convert it to 180 degree ranges, then use r,g,b as if it is temperature,
// red being highest tension, blue being lowest tension.
float t = (kind == areaViewTension) ? cGap : masterTension;
t = max(0, 180 * (1 - t / 40));
float r = (t < 90) ? 255 * cos(radians(t)) : 0;
float g = 255 * sin(radians(t));
float b = (t >= 90) ? 255 * -cos(radians(t)) : 0;
drawPoints(img, color(r, g, b));
break;
}
case areaViewTexture: // Draw the color of the patches in texture drawing
{
if (texture == null ||
textureType != ctrlTextureType || textureAxis != ctrlTextureAxis ||
textureGap != ctrlTextureGap || textureBG != ctrlTextureBG)
{ // the cache doesn't exist or is no longer valid; re-create the texture
// Identify background, foreground color and the density to make mColor
MunsellColor mcBackground = null;
MunsellColor mcForeground = null;
float density = 0;
switch (ctrlTextureAxis) {
case txtAxisHue: // hue
{
// Find the unit vector that is perpendicular to the current color (mColor)
float ux = cos(radians(mColor.hueDegree)), uy = sin(radians(mColor.hueDegree));
PVector left = new PVector(-uy, ux);
PVector right = new PVector(uy, -ux);
// Generate ordered pair of left/right gap so that
// max gap is preferred and the mid-point is preferred within the same gap
ArrayList<PVector> tries = new ArrayList<PVector>();
for (int g = ctrlTextureGap; g > 0; g--) {
for (int h = g / 2; h >= 0; h--) {
tries.add(new PVector(h, g - h));
tries.add(new PVector(g - h, h));
}
}
// Find a valid pair in the Munsell sphere.
int bg = 0, fg = 0;
PVector cp = mColor.getCoordinate();
for (PVector t : tries) {
PVector lp = PVector.mult(left, t.x).add(cp);
PVector rp = PVector.mult(right, t.y).add(cp);
if (colorTable.isMunsellKeyInMap(new MunsellColor(lp)) &&
colorTable.isMunsellKeyInMap(new MunsellColor(rp)))
{
bg = (int)t.x;
fg = (int)t.y;
break;
}
}
if (bg + fg > 0)
density = (float)bg / (bg + fg);
if (ctrlTextureBG == txtBGHigh) {
mcBackground = new MunsellColor(PVector.mult(left, bg).add(cp));
mcForeground = new MunsellColor(PVector.mult(right, fg).add(cp));
}
else {
mcBackground = new MunsellColor(PVector.mult(right, fg).add(cp));
mcForeground = new MunsellColor(PVector.mult(left, bg).add(cp));
density = 1.0 - density;
}
break;
}
case txtAxisValue: // value
case txtAxisChroma: // chroma
default:
{
int lb, ub; // lower/upper bound of the range of H/V/C being considered
int bg, fg; // foreground/background of the range of H/V/C being considered
int ctr;
int gap = 0;
if (ctrlTextureAxis == txtAxisChroma) {
ub = colorTable.getMaxChroma(mColor.hueDegree, mColor.value);
lb = 0;
ctr = round(mColor.chroma);
}
else {
ub = colorTable.getMaxValue(mColor.hueDegree, mColor.chroma);
lb = colorTable.getMinValue(mColor.hueDegree, mColor.chroma);
ctr = round(mColor.value);
}
bg = fg = ctr;
gap = 0;
while (gap < ctrlTextureGap && (bg < ub || fg > lb) ) { // adjust bg/fg by the gap amount
if (bg < ub) { bg++; gap++; }
if (fg > lb && gap < ctrlTextureGap) { fg--; gap++; }
}
if (bg > fg)
density = (float)(bg - ctr) / (bg - fg);
if (ctrlTextureBG == txtBGLow) { // flip bg/fg and density so it uses the lower H/V/C as background
int t = bg; bg = fg; fg = t;
density = 1.0 - density;
}
mcBackground = new MunsellColor(mColor);
mcForeground = new MunsellColor(mColor);
if (ctrlTextureAxis == txtAxisChroma) {
mcBackground.chroma = bg;
mcForeground.chroma = fg;
}
else {
mcBackground.value = bg;
mcForeground.value = fg;
}
break;
}
}
PGraphics buffer = drawTexture(owner, id, mcBackground, mcForeground, density);
texture = new color[points.size()];
for (int i = 0; i < points.size(); i++) {
PVector p = points.get(i);
texture[i] = buffer.get((int)(p.x - xMin), (int)(p.y - yMin));
}
textureType = ctrlTextureType;
textureAxis = ctrlTextureAxis;
textureGap = ctrlTextureGap;
textureBG = ctrlTextureBG;
}
drawPoints(img, colorNone);
break;
}
case areaViewMTransition: // Draw master transition - handled at painting level
default:
break;
}
}
// Draw the color patch to the image with given color
public void drawPoints(PImage img, color c)
{
boolean bTexture = (c == colorNone);
// Draw area inside
if (bTexture && texture != null) { // use texture
for (int i = 0; i < points.size(); i++) {
PVector p = points.get(i);
img.set((int)p.x, (int)p.y, texture[i]);
}
}
else {
for (PVector p : points) {
img.set((int)p.x, (int)p.y, c);
}
}
// Draw contour
if (bShowContour) {
for (PVector p : contourPoints) {
img.set((int)p.x, (int)p.y, (bTexture) ? colorBG : colorEdge);
}
}
}
}