Advertisement
Guest User

Untitled

a guest
May 25th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. PImage srcImg;
  2. PImage dstImg;
  3. color[] palette = {
  4. color(0xF0, 0xF0, 0xF0),
  5. color(0xF2, 0xB2, 0x33),
  6. color(0xE5, 0x7F, 0xD8),
  7. color(0x99, 0xB2, 0xF2),
  8. color(0xDE, 0xDE, 0x6C),
  9. color(0x7F, 0xCC, 0x19),
  10. color(0xF2, 0xB2, 0xCC),
  11. color(0x4C, 0x4C, 0x4C),
  12. color(0x99, 0x99, 0x99),
  13. color(0x4C, 0x99, 0xB2),
  14. color(0xB2, 0x66, 0xE5),
  15. color(0x25, 0x31, 0x92),
  16. color(0x7F, 0x66, 0x4C),
  17. color(0x57, 0xA6, 0x4E),
  18. color(0xCC, 0x4C, 0x4C),
  19. color(0x19, 0x19, 0x19),
  20. };
  21.  
  22. void displayColor(color c, float x, float y) {
  23. fill(c);
  24. rect(x, y, 20, 20);
  25. fill(color(1.0, 1.0, 1.0));
  26. textSize(14);
  27. text("(" + nf(hue(c), 1, 2) + "," +
  28. nf(saturation(c), 1, 2) + "," +
  29. nf(brightness(c), 1, 2) + ")",
  30. x + 25, y + 16);
  31. }
  32.  
  33. float rgbDist(color c1, color c2) {
  34. float rd = red(c2) - red(c1);
  35. float gd = green(c2) - green(c1);
  36. float bd = blue(c2) - blue(c1);
  37. return rd * rd + gd * gd + bd * bd;
  38. }
  39.  
  40. float hsbDist(color c1, color c2) {
  41. float hd = hue(c2) - hue(c1);
  42. float sd = saturation(c2) - saturation(c1);
  43. float bd = brightness(c2) - brightness(c1);
  44. if (0.5 < abs(hd)) {
  45. hd = 1.0 - abs(hd);
  46. }
  47. return sqrt(hd * hd) + 0.05 * sqrt(sd * sd) + 0.01 * sqrt(bd * bd);
  48. }
  49.  
  50. color findColor(color c) {
  51. color result = palette[0];
  52. float min_dist = MAX_FLOAT;
  53. for (int i = 0; i < palette.length; i++) {
  54. float d;
  55. if (saturation(c) < 0.25 || brightness(c) < 0.25) {
  56. d = rgbDist(palette[i], c);
  57. } else {
  58. d = hsbDist(palette[i], c);
  59. }
  60. if (min_dist > d) {
  61. min_dist = d;
  62. result = palette[i];
  63. }
  64. }
  65. return result;
  66. }
  67.  
  68. void setup() {
  69. size(640, 480);
  70. colorMode(RGB, 1.0, 1.0, 1.0);
  71. srcImg = loadImage("face.png", "png");
  72. dstImg = createImage(srcImg.width, srcImg.height, RGB);
  73.  
  74. srcImg.loadPixels();
  75. for (int i = 0; i < srcImg.width * srcImg.height; i++) {
  76. dstImg.pixels[i] = findColor(srcImg.pixels[i]);
  77. }
  78. dstImg.updatePixels();
  79. }
  80.  
  81. void draw() {
  82. background(0);
  83.  
  84. float lm = 25;
  85. float tm = 5;
  86. displayColor(palette[0], 0 + lm, 0 + tm);
  87. displayColor(palette[1], 140 + lm, 0 + tm);
  88. displayColor(palette[2], 280 + lm, 0 + tm);
  89. displayColor(palette[3], 420 + lm, 0 + tm);
  90. displayColor(palette[4], 0 + lm, 25 + tm);
  91. displayColor(palette[5], 140 + lm, 25 + tm);
  92. displayColor(palette[6], 280 + lm, 25 + tm);
  93. displayColor(palette[7], 420 + lm, 25 + tm);
  94. displayColor(palette[8], 0 + lm, 50 + tm);
  95. displayColor(palette[9], 140 + lm, 50 + tm);
  96. displayColor(palette[10], 280 + lm, 50 + tm);
  97. displayColor(palette[11], 420 + lm, 50 + tm);
  98. displayColor(palette[12], 0 + lm, 75 + tm);
  99. displayColor(palette[13], 140 + lm, 75 + tm);
  100. displayColor(palette[14], 280 + lm, 75 + tm);
  101. displayColor(palette[15], 420 + lm, 75 + tm);
  102.  
  103. float srcX = (width - srcImg.width) / 2;
  104. float srcY = 110;
  105. image(srcImg, srcX, srcY);
  106.  
  107. float dstX = (width - dstImg.width) / 2;
  108. float dstY = 110 + 172 + 10;
  109. image(dstImg, dstX, dstY);
  110.  
  111. if ((dstX <= mouseX && mouseX < dstX + dstImg.width &&
  112. dstY <= mouseY && mouseY < dstY + dstImg.height) ||
  113. (srcX <= mouseX && mouseX < srcX + srcImg.width &&
  114. srcY <= mouseY && mouseY < srcY + srcImg.height)) {
  115. color c = get(mouseX, mouseY);
  116. text("RGB(" +
  117. nf(red(c), 1, 2) + "," +
  118. nf(green(c), 1, 2) + "," +
  119. nf(blue(c), 1, 2) + ")",
  120. srcX + srcImg.width + 20, 130);
  121. text("HSB(" +
  122. nf(hue(c), 1, 2) + "," +
  123. nf(saturation(c), 1, 2) + "," +
  124. nf(brightness(c), 1, 2) + ")",
  125. srcX + srcImg.width + 20, 150);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement