Debian Codegraph @

tools/castle-editor/components/mbColorLib/RGBCMYKUtils.pas

raw
1 unit RGBCMYKUtils;
2
3 interface
4
5 // Activate only one of these defines - see comments below
6
7 {.$DEFINE CMYK_FORMULA_1} // Original formula used by mbColorLib
8 {$DEFINE CMYK_FORMULA_2} // Result agrees with OpenOffice
9
10 { Compiling this causes a number of unavoidable FPC notes,
11
12 Note: (6058) Call to subroutine "operator =(const op1:Variant;const op2:Variant):Boolean;" marked as inline is not inlined
13 .... }
14 {$notes off}
15
16 uses
17 LCLIntf, Graphics, Math;
18
19 function CMYtoColor(C, M, Y: integer): TColor;
20 procedure RGBtoCMY(clr: TColor; var C, M, Y: integer);
21
22 function CMYKToColor (C, M, Y, K: Integer): TColor;
23 procedure ColorToCMYK(clr: TColor; out C, M, Y, K: integer);
24
25 function GetCValue(c: TColor): integer;
26 function GetMValue(c: TColor): integer;
27 function GetYValue(c: TColor): integer;
28 function GetKValue(c: TColor): integer;
29
30
31 implementation
32
33 function CMYtoColor(C, M, Y: integer): TColor;
34 begin
35 Result := RGB(255 - C, 255 - M, 255 - Y);
36 end;
37
38 procedure RGBtoCMY(clr: TColor; var C, M, Y: integer);
39 begin
40 C := 255 - GetRValue(clr);
41 M := 255 - GetGValue(clr);
42 Y := 255 - GetBValue(clr);
43 end;
44
45 {$IFDEF CMYK_FORMULA_1}
46 //==============================================================================
47 // Original formulas of mbColorLib
48 //==============================================================================
49 function CMYKtoColor(C, M, Y, K: Integer): TColor;
50 begin
51 Result := RGBtoColor(
52 (255 - (C + K)) mod 255, // wp: added mod 255, otherwise the result is nonsense
53 (255 - (M + K)) mod 255,
54 (255 - (Y + K)) mod 255
55 );
56 end;
57
58 procedure ColorToCMYK(clr: TColor; out C, M, Y, K: integer);
59 begin
60 C := 255 - GetRValue(clr);
61 M := 255 - GetGValue(clr);
62 Y := 255 - GetBValue(clr);
63 K := MinIntValue([C, M, Y]);
64 C := C - K;
65 M := M - K;
66 Y := Y - K;
67 end;
68 {$ENDIF}
69
70 {$IFDEF CMYK_FORMULA_2}
71 //==============================================================================
72 // Other formulas
73 // http://www.rapidtables.com/convert/color/cmyk-to-rgb.htm
74 // or https://stackoverflow.com/questions/2426432/convert-rgb-color-to-cmyk
75 //
76 // Result agrees with OpenOffice.
77 //==============================================================================
78 function CMYKtoColor(C, M, Y, K: Integer): TColor;
79 begin
80 Result := RGBtoColor(
81 (255-C) * (255-K) div 255,
82 (255-M) * (255-K) div 255,
83 (255-Y) * (255-K) div 255
84 );
85 end;
86
87 procedure ColorToCMYK(clr: TColor; out C, M, Y, K: Integer);
88 var
89 r, g, b: Integer;
90 r1, g1, b1, c1, m1, y1, k1: Double;
91 begin
92 r := GetRValue(clr);
93 g := GetGValue(clr);
94 b := GetBValue(clr);
95 if (r = 0) and (g = 0) and (b = 0) then
96 begin
97 C := 0;
98 M := 0;
99 Y := 0;
100 K := 1;
101 exit;
102 end;
103 r1 := r / 255;
104 g1 := g / 255;
105 b1 := b / 255;
106 k1 := MinValue([1-r1, 1-g1, 1-b1]);
107 c1 := (1 - r1 - k1) / (1 - k1);
108 m1 := (1 - g1 - k1) / (1 - k1);
109 y1 := (1 - b1 - k1) / (1 - k1);
110 C := round(255 * c1);
111 M := round(255 * m1);
112 Y := round(255 * y1);
113 K := round(255 * k1);
114 end;
115 {$ENDIF}
116
117 //==============================================================================
118
119 function GetCValue(c: TColor): integer;
120 var
121 d: integer;
122 begin
123 ColorToCMYK(c, Result, d, d, d);
124 end;
125
126 function GetMValue(c: TColor): integer;
127 var
128 d: integer;
129 begin
130 ColorToCMYK(c, d, Result, d, d);
131 end;
132
133 function GetYValue(c: TColor): integer;
134 var
135 d: integer;
136 begin
137 ColorToCMYK(c, d, d, Result, d);
138 end;
139
140 function GetKValue(c: TColor): integer;
141 var
142 d: integer;
143 begin
144 ColorToCMYK(c, d, d, d, Result);
145 end;
146
147 end.