tags:

views:

105

answers:

2

Is it possible to create an aspx page where the user enters CMYK values, and the CMYK colour will be displayed?

I've done some googling and the System.Drawing library seems to use RGB.

+1  A: 

No, this is not possible. Both HTML/CSS and .NET's System.Drawing are RGB-based. And although you can use a color converter to convert CMYK to RGB, it will not be a perfect 1-on-1 mapping, because there are colors in the CMYK color space that cannot be represented in RGB and the other way around.

However, to be a little more solution oriented: the following code will give you a simple conversion:

CMYK to RGB

int r = (int) 255 * (1 - (c * (1 - k) + k));
int g = (int) 255 * (1 - (m * (1 - k) + k));
int b = (int) 255 * (1 - (y * (1 - k) + k));

RGB to CMYK

float c = 1 - r / 255f;
float m = 1 - g / 255f;
float y = 1 - b / 255f;
float k = c;
if (k > m)
    k = m;
if (k > y)
    k = y;

if (k == 1)
{
    c = 0; m = 0; y = 0;
}
else
{
    c = (c - k) / (1 - k);
    m = (m - k) / (1 - k);
    y = (y - k) / (1 - k);
}

Note that c, y, m and k will be in the range 0 - 1 (i.e., 0% - 100%), and r, g, b in the range 0 - 255.

Ruben
Shouldn't it be converting cmyk to rgb?
PaulB
Sorry, temporarily had my wires crossed. I'll adjust the sample code.
Ruben
+1  A: 

You can't directly. This will give a good approximation from cmyk to rgb

  private Color CMYKtoRBG(float c, float m, float y, float k)
  {
      float r = Math.Min(1, c * (1 - k) + k);
      float g = Math.Min(1, m * (1 - k) + k);
      float b = Math.Min(1, y * (1 - k) + k);
      return Color.FromArgb(255-(int)(r * 255),255- (int)(g * 255), 255-(int)(b * 255));
  }
PaulB