RGB/CMYK Conversion


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. void RGBtoCMYK(float color[4])
  2. {
  3. if (color[0]==0 && color[1]==0 && color[2]==0)
  4. {
  5. color[0] = 0;
  6. color[1] = 0;
  7. color[2] = 0;
  8. color[3] = 1.0;
  9. }
  10. else if (color[0]==1.0 && color[1]==1.0 && color[2]==1.0)
  11. {
  12. color[0] = 0;
  13. color[1] = 0;
  14. color[2] = 0;
  15. color[3] = 0;
  16. }
  17. else
  18. {
  19. color[0] = 1.0 - color[0];
  20. color[1] = 1.0 - color[1];
  21. color[2] = 1.0 - color[2];
  22.  
  23. float minK = MIN(color[0], MIN(color[1], color[2]));
  24. color[0] = (color[0] - minK) / (1.0 - minK);
  25. color[1] = (color[1] - minK) / (1.0 - minK);
  26. color[2] = (color[2] - minK) / (1.0 - minK);
  27. color[3] = minK;
  28. }
  29. }
  30.  
  31. void CMYKtoRGB(float color[4])
  32. {
  33. color[0] = 1.0 - (color[0] * (1.0 - color[3]) + color[3]);
  34. color[1] = 1.0 - (color[1] * (1.0 - color[3]) + color[3]);
  35. color[2] = 1.0 - (color[2] * (1.0 - color[3]) + color[3]);
  36. color[3] = 1.0;
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.