0

I have CMYK color values ( 0, 0.58 ,1 ,0 ) . Now I have to convert to its Integer equivalent using C# . I think it is possible using Bitwise operator but not sure .

Kindly assist me how can achieve same .

Thanks, Pawan

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user1641519
  • 377
  • 1
  • 4
  • 17
  • If you only want to convert them to int, you can use Convert.ToInt32 method. But that would make you lose the precision. Did you maybe think of converting them to RGB colorspace? – Legoless Sep 02 '12 at 07:46
  • @Legoless no, I think I understood what the OP wanted, see my answer. –  Sep 02 '12 at 07:50

1 Answers1

2

Try this:

float c = 0.0;
float y = 0.58;
float m = 1.0;
float k = 0.0;

uint intColor = (uint)(c * 255) << 24;
intColor += (uint)(y * 255) << 16;
intColor += (uint)(m * 255) << 8;
intColor += (uint)(k * 255) << 0;

Here intColor will be a 32-bit unsigned integer, containing the byte value of the C, Y, M and K components of the color, respectively. To convert back to the components from the integer, simply invert all the operations and their order:

float c = ((intColor & 0xFF000000) >> 24) / 255.0f;
float y = ((intColor & 0x00FF0000) >> 16) / 255.0f;
float m = ((intColor & 0x0000FF00) >> 8) / 255.0f;
float k = ((intColor & 0x000000FF) >> 0) / 255.0f;
  • Thanks... Can you tell me how to convert from back from int to CMYK equivalent ? – user1641519 Sep 04 '12 at 06:36
  • @userXXX you keep mentioning this 'equivalent' persistently... But what is it? What do you want, precisely? –  Sep 04 '12 at 06:49
  • Hello, Suppose I have CMYK color values ( 0, 0.58 ,1 ,0 ) Now i have to convert it into Int and than later I will have to get from Int values to CMYK values. Here I want back and forth conversion from CMYK to Int and Vice-versa . CMYK represnt the color values.so int also should reprensent the equivalnet color value . yes I want precisely. – user1641519 Sep 04 '12 at 07:05
  • Also, The code above ( for conversion from CMYK to int ) , the OutPut is stored as Uint ... is that correct ... kindly assist me. – user1641519 Sep 04 '12 at 07:29
  • @userXXX I see. Also, I rarely write incorrect answers. In fact, it's better to use an UInt conceptionally - bitwise operations are safer and a color component cannot be negative. –  Sep 04 '12 at 07:37
  • why work with /255 or /256? CMYK are percentages (2 decimals?) – nl-x Jan 14 '14 at 14:13