Mega Code Archive

 
Categories / Delphi / Graphic
 

Converting a RGB color to a CMYK color

Title: Converting a RGB color to a CMYK color Question: How do I convert an RGB color to a CMYK color? Answer: The following functions RGBTOCMYK() and CMYKTORGB() demonstrate how to convert between RGB and CMYK color spaces. Note: There is a direct relationship between RGB colors and CMY colors. In a CMY color, black tones are achieved by printing equal amounts of Cyan, Magenta, and Yellow ink. The black component in a CMY color is achieved by reducing the CMY components by the minimum of (C, M, and Y) and substituting pure black in its place producing a sharper print and using less ink. Since it is possible for a user to boost the C,M and Y components where boosting the black component would have been preferable, a ColorCorrectCMYK() function is provided to achieve the same color by reducing the C, M and Y components, and boosting the K component. Example: procedure RGBTOCMYK(R : byte; G : byte; B : byte; var C : byte; var M : byte; var Y : byte; var K : byte); begin C := 255 - R; M := 255 - G; Y := 255 - B; if C K := C else K := M; if Y K := Y; if k 0 then begin c := c - k; m := m - k; y := y - k; end; end; procedure CMYKTORGB(C : byte; M: byte; Y : byte; K : byte; var R : byte; var G : byte; var B : byte); begin if (Integer(C) + Integer(K)) R := 255 - (C + K) else R := 0; if (Integer(M) + Integer(K)) G := 255 - (M + K) else G := 0; if (Integer(Y) + Integer(K)) B := 255 - (Y + K) else B := 0; end; procedure ColorCorrectCMYK(var C : byte; var M : byte; var Y : byte; var K : byte); var MinColor : byte; begin if C MinColor := C else MinColor := M; if Y MinColor := Y; if MinColor + K 255 then MinColor := 255 - K; C := C - MinColor; M := M - MinColor; Y := Y - MinColor; K := K + MinColor; end; procedure TForm1.Button1Click(Sender: TObject); var R : byte; G : byte; B : byte; C : byte; M : byte; Y : byte; K : byte; begin R := 151; G := 81; B := 55; Memo1.Lines.Add('R = ' + IntToStr(R)); Memo1.Lines.Add('G = ' + IntToStr(G)); Memo1.Lines.Add('B = ' + IntToStr(B)); Memo1.Lines.Add('-------------------'); RGBTOCMYK(R, G, B, C, M, Y, K); Memo1.Lines.Add('C = ' + IntToStr(C)); Memo1.Lines.Add('M = ' + IntToStr(M)); Memo1.Lines.Add('Y = ' + IntToStr(Y)); Memo1.Lines.Add('K = ' + IntToStr(K)); Memo1.Lines.Add('-------------------'); CMYKTORGB(C, M, Y, K, R, G, B); Memo1.Lines.Add('R = ' + IntToStr(R)); Memo1.Lines.Add('G = ' + IntToStr(G)); Memo1.Lines.Add('B = ' + IntToStr(B)); Memo1.Lines.Add('-------------------'); RGBTOCMYK(R, G, B, C, M, Y, K); c := c + 2; m := m + 2; y := y + 2; ColorCorrectCMYK(C, M, Y, K); Memo1.Lines.Add('C = ' + IntToStr(C)); Memo1.Lines.Add('M = ' + IntToStr(M)); Memo1.Lines.Add('Y = ' + IntToStr(Y)); Memo1.Lines.Add('K = ' + IntToStr(K)); end;