djjd47130

Delphi TColorRec record (RGB, CMYK, HSV support)

Jan 30th, 2015
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.33 KB | None | 0 0
  1. (*
  2.   TColorRec - Helper record for TColor to easily interchange for simplicity of reading not only RGB but also CMYK and HSV.
  3.   Supports directly assigning a TColor using implicit class operators.
  4. *)
  5.  
  6. type
  7.   TColorRec = record
  8.   private
  9.     FRed: Byte;
  10.     FGreen: Byte;
  11.     FBlue: Byte;
  12.     function GetBrightness: Double;
  13.     function GetHue: Double;
  14.     function GetSaturation: Double;
  15.     procedure SetBrightness(const Value: Double);
  16.     procedure SetHue(const Value: Double);
  17.     procedure SetSaturation(const Value: Double);
  18.     function GetBlack: Integer;
  19.     function GetCyan: Integer;
  20.     function GetMagenta: Integer;
  21.     function GetYellow: Integer;
  22.     procedure SetBlack(const Value: Integer);
  23.     procedure SetCyan(const Value: Integer);
  24.     procedure SetMagenta(const Value: Integer);
  25.     procedure SetYellow(const Value: Integer);
  26.   public
  27.     class operator implicit(Value: TColorRec): TColor;
  28.     class operator implicit(Value: TColor): TColorRec;
  29.     property Red: Byte read FRed write FRed;
  30.     property Green: Byte read FGreen write FGreen;
  31.     property Blue: Byte read FBlue write FBlue;
  32.     property Hue: Double read GetHue write SetHue;
  33.     property Saturation: Double read GetSaturation write SetSaturation;
  34.     property Brightness: Double read GetBrightness write SetBrightness;
  35.     property Cyan: Integer read GetCyan write SetCyan;
  36.     property Magenta: Integer read GetMagenta write SetMagenta;
  37.     property Yellow: Integer read GetYellow write SetYellow;
  38.     property Black: Integer read GetBlack write SetBlack;
  39.   end;
  40.  
  41. implementation
  42.  
  43. function RGBToHSV(R, G, B: Byte; var H, S, V: Double): Boolean;
  44. var
  45.   minRGB, maxRGB, delta: Double;
  46. begin
  47.   h := 0.0;
  48.   minRGB := Min(Min(R, G), B);
  49.   maxRGB := Max(Max(R, G), B);
  50.   delta := (maxRGB - minRGB);
  51.   V := maxRGB;
  52.   if (maxRGB <> 0.0) then
  53.     S := 255.0 * delta / maxRGB
  54.   else
  55.     S := 0.0;
  56.   if (S <> 0.0) then begin
  57.     if R = maxRGB then
  58.       H := (G - B) / delta
  59.     else if G = maxRGB then
  60.       H := 2.0 + (B - R) / delta
  61.     else if B = maxRGB then
  62.       H := 4.0 + (R - G) / delta
  63.   end else
  64.     H := -1.0;
  65.   H := h * 60;
  66.   if H < 0.0 then
  67.     H := H + 360.0;
  68.  
  69.   //S := S * 100 / 255;
  70.   //V := B * 100 / 255;
  71.   S := S / 255;
  72.   V := V / 255;
  73.  
  74.   Result:= True;
  75. end;
  76.  
  77. function HSVToRGB(H, S, V: Double; var R, G, B: Byte): Boolean;
  78. var
  79.   i: Integer;
  80.   f, p, q, t: Double;
  81.   procedure CopyOutput(const RV, GV, BV: Double);
  82.   const
  83.     RGBmax = 255;
  84.   begin
  85.     R:= Round(RGBmax * RV);
  86.     G:= Round(RGBmax * GV);
  87.     B:= Round(RGBmax * BV);
  88.   end;
  89. begin
  90.   Assert(InRange(H, 0.0, 1.0));
  91.   Assert(InRange(S, 0.0, 1.0));
  92.   Assert(InRange(V, 0.0, 1.0));
  93.   if S = 0.0 then begin
  94.     // achromatic (grey)
  95.     CopyOutput(B, B, B);
  96.     Result:= True;
  97.     exit;
  98.   end;
  99.   H := H * 6.0; // sector 0 to 5
  100.   i := floor(H);
  101.   f := H - i; // fractional part of H
  102.   p := V * (1.0 - S);
  103.   q := V * (1.0 - S * f);
  104.   t := V * (1.0 - S * (1.0 - f));
  105.   case i of
  106.     0: CopyOutput(V, t, p);
  107.     1: CopyOutput(q, V, p);
  108.     2: CopyOutput(p, V, t);
  109.     3: CopyOutput(p, q, V);
  110.     4: CopyOutput(t, p, V);
  111.     else CopyOutput(V, p, q);
  112.   end;
  113.   Result:= True;
  114. end;
  115.  
  116. { TColorRec }
  117.  
  118. class operator TColorRec.implicit(Value: TColorRec): TColor;
  119. begin
  120.   with Value do
  121.     Result:= RGB(Red, Green, Blue);
  122. end;
  123.  
  124. class operator TColorRec.implicit(Value: TColor): TColorRec;
  125. begin
  126.   with Result do begin
  127.     FRed:= GetRValue(Value);
  128.     FGreen:= GetGValue(Value);
  129.     FBlue:= GetBValue(Value);
  130.   end;
  131. end;
  132.  
  133. function TColorRec.GetHue: Double;
  134. var
  135.   H, S, V: Double;
  136. begin
  137.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  138.   Result:= H;
  139. end;
  140.  
  141. function TColorRec.GetSaturation: Double;
  142. var
  143.   H, S, V: Double;
  144. begin
  145.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  146.   Result:= S;
  147. end;
  148.  
  149. function TColorRec.GetBrightness: Double;
  150. var
  151.   H, S, V: Double;
  152. begin
  153.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  154.   Result:= V;
  155. end;
  156.  
  157. function TColorRec.GetCyan: Integer;
  158. begin
  159.   Result:= GetCValue(RGB(FRed, FGreen, FBlue));
  160. end;
  161.  
  162. function TColorRec.GetMagenta: Integer;
  163. begin
  164.   Result:= GetMValue(RGB(FRed, FGreen, FBlue));
  165. end;
  166.  
  167. function TColorRec.GetYellow: Integer;
  168. begin
  169.   Result:= GetYValue(RGB(FRed, FGreen, FBlue));
  170. end;
  171.  
  172. function TColorRec.GetBlack: Integer;
  173. begin
  174.   Result:= GetKValue(RGB(FRed, FGreen, FBlue));
  175. end;
  176.  
  177. procedure TColorRec.SetBrightness(const Value: Double);
  178. var
  179.   H, S, V: Double;
  180. begin
  181.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  182.   V:= Value;
  183.   HSVToRGB(H, S, V, FRed, FGreen, FBlue);
  184. end;
  185.  
  186. procedure TColorRec.SetHue(const Value: Double);
  187. var
  188.   H, S, V: Double;
  189. begin
  190.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  191.   H:= Value;
  192.   HSVToRGB(H, S, V, FRed, FGreen, FBlue);
  193. end;
  194.  
  195. procedure TColorRec.SetSaturation(const Value: Double);
  196. var
  197.   H, S, V: Double;
  198. begin
  199.   RGBToHSV(FRed, FGreen, FBlue, H, S, V);
  200.   S:= Value;
  201.   HSVToRGB(H, S, V, FRed, FGreen, FBlue);
  202. end;
  203.  
  204. procedure TColorRec.SetCyan(const Value: Integer);
  205. begin
  206.   Self:= CMYK(Value, Magenta, Yellow, Black);
  207. end;
  208.  
  209. procedure TColorRec.SetMagenta(const Value: Integer);
  210. begin
  211.   Self:= CMYK(Cyan, Value, Yellow, Black);
  212. end;
  213.  
  214. procedure TColorRec.SetYellow(const Value: Integer);
  215. begin
  216.   Self:= CMYK(Cyan, Magenta, Value, Black);
  217. end;
  218.  
  219. procedure TColorRec.SetBlack(const Value: Integer);
  220. begin
  221.   Self:= CMYK(Cyan, Magenta, Yellow, Value);
  222. end;
Advertisement
Add Comment
Please, Sign In to add comment