Loading...
Logo
Processing Forum
Hi there all. I am currently using the following lines of code, to extract greyscale values from pixels in an image:

 color c = get(x, y);
 float greyscalein = brightness(c);

i have moved my project (a plotter, of sorts) onto working colour. therefore i would like to extract colour values instead.

Ideally in CMYK, but i'm sure it could be converted if it is in a different format.
i presume i need to change the "brightness" bit to something else, except what i really want is not to see what hue the pixel is, but rather, on 3 passes, HOW MUCH C, how much M and how much Y is on the pixel

any suggestions?

many thanks

Ol

Replies(7)

Doing CMYK is actually fairly difficult, everything is set up for RGB. I set up a sketch that gives the RGB value wherever you click. I wrote an explanation on why this works recently, I'll try to find it in a minute:
Copy code
  1. void setup() {
  2.   size(400, 400, P2D);
  3.   noStroke();
  4.   for (int i = 0 ; i < 40; i++) {
  5.     for (int j = 0 ; j < 40; j++) {
  6.       fill(random(255), random(255), random(255));
  7.       rect(i*10, j*10, 10, 10);
  8.     }
  9.   }
  10. }

  11. void draw() {
  12. }

  13. void mousePressed() {
  14.   loadPixels();
  15.   int c = pixels[mouseX+mouseY*width];
  16.   int r = (c&0xff0000)>>16;
  17.   int g = (c&0xff00)>>8;
  18.   int b = c&0xff;
  19.   println("Mouse:("+mouseX+", "+mouseY+") RGB:("+r+", "+g+", "+b+")");
  20. }
Brightness: see the function of same name in the Reference, and look at colorMode() too (HSB).
could i plot each individual colour (or combination of colours) over a black background, and then take the brightness.
ie: combine red and blue, over a black background, therefore effective brightness would be magenta?
Brightness is different from color, it actually removes any color and produces a value (grayscale).

The code I placed takes the color at (mouseX, mouseY) in RGB. It would be worth while to understand why RGB is convenient in Processing. If you want the color at some arbitrary (x, y) then just replace mouseX and mouseY in the code.

Edit: I'm not sure why you'd want to plot this on a black background. You could calculate the brightness or combined color without plotting anything.
perhaps i missed out a crucial piece of information.

the data is to be used to plot an image onto paper.
The setup currently pulls out greyscale (brightness), and plots it resulting in a pretty good black and white likeness.
I have modified the setup so it can loop over as many times as needs be, through all the colours required.
being additive rather than subtractive colours (or is it the other way around) rather than RGB,
So the resultant image data needs to look a bit like this:



cycling once for each colour, and plotting full colour as 255, white as 0....

as they are difference cycles, can i change the "colour mode" for each cycle?
so plot the saturation on each cycle, but limit the hues availible?
In other words, you need CMYK. As indicated by the posts above, Processing doesn't support CMYK natively (only RGB and HSB), which means you either write your own RGB to CMYK conversion methods (for formula's see google) or you use a library such as Toxiclibs which you can use to easily extract CMYK values from a color, see the TColor class.