Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- TColorRec - Helper record for TColor to easily interchange for simplicity of reading not only RGB but also CMYK and HSV.
- Supports directly assigning a TColor using implicit class operators.
- *)
- type
- TColorRec = record
- private
- FRed: Byte;
- FGreen: Byte;
- FBlue: Byte;
- function GetBrightness: Double;
- function GetHue: Double;
- function GetSaturation: Double;
- procedure SetBrightness(const Value: Double);
- procedure SetHue(const Value: Double);
- procedure SetSaturation(const Value: Double);
- function GetBlack: Integer;
- function GetCyan: Integer;
- function GetMagenta: Integer;
- function GetYellow: Integer;
- procedure SetBlack(const Value: Integer);
- procedure SetCyan(const Value: Integer);
- procedure SetMagenta(const Value: Integer);
- procedure SetYellow(const Value: Integer);
- public
- class operator implicit(Value: TColorRec): TColor;
- class operator implicit(Value: TColor): TColorRec;
- property Red: Byte read FRed write FRed;
- property Green: Byte read FGreen write FGreen;
- property Blue: Byte read FBlue write FBlue;
- property Hue: Double read GetHue write SetHue;
- property Saturation: Double read GetSaturation write SetSaturation;
- property Brightness: Double read GetBrightness write SetBrightness;
- property Cyan: Integer read GetCyan write SetCyan;
- property Magenta: Integer read GetMagenta write SetMagenta;
- property Yellow: Integer read GetYellow write SetYellow;
- property Black: Integer read GetBlack write SetBlack;
- end;
- implementation
- function RGBToHSV(R, G, B: Byte; var H, S, V: Double): Boolean;
- var
- minRGB, maxRGB, delta: Double;
- begin
- h := 0.0;
- minRGB := Min(Min(R, G), B);
- maxRGB := Max(Max(R, G), B);
- delta := (maxRGB - minRGB);
- V := maxRGB;
- if (maxRGB <> 0.0) then
- S := 255.0 * delta / maxRGB
- else
- S := 0.0;
- if (S <> 0.0) then begin
- if R = maxRGB then
- H := (G - B) / delta
- else if G = maxRGB then
- H := 2.0 + (B - R) / delta
- else if B = maxRGB then
- H := 4.0 + (R - G) / delta
- end else
- H := -1.0;
- H := h * 60;
- if H < 0.0 then
- H := H + 360.0;
- //S := S * 100 / 255;
- //V := B * 100 / 255;
- S := S / 255;
- V := V / 255;
- Result:= True;
- end;
- function HSVToRGB(H, S, V: Double; var R, G, B: Byte): Boolean;
- var
- i: Integer;
- f, p, q, t: Double;
- procedure CopyOutput(const RV, GV, BV: Double);
- const
- RGBmax = 255;
- begin
- R:= Round(RGBmax * RV);
- G:= Round(RGBmax * GV);
- B:= Round(RGBmax * BV);
- end;
- begin
- Assert(InRange(H, 0.0, 1.0));
- Assert(InRange(S, 0.0, 1.0));
- Assert(InRange(V, 0.0, 1.0));
- if S = 0.0 then begin
- // achromatic (grey)
- CopyOutput(B, B, B);
- Result:= True;
- exit;
- end;
- H := H * 6.0; // sector 0 to 5
- i := floor(H);
- f := H - i; // fractional part of H
- p := V * (1.0 - S);
- q := V * (1.0 - S * f);
- t := V * (1.0 - S * (1.0 - f));
- case i of
- 0: CopyOutput(V, t, p);
- 1: CopyOutput(q, V, p);
- 2: CopyOutput(p, V, t);
- 3: CopyOutput(p, q, V);
- 4: CopyOutput(t, p, V);
- else CopyOutput(V, p, q);
- end;
- Result:= True;
- end;
- { TColorRec }
- class operator TColorRec.implicit(Value: TColorRec): TColor;
- begin
- with Value do
- Result:= RGB(Red, Green, Blue);
- end;
- class operator TColorRec.implicit(Value: TColor): TColorRec;
- begin
- with Result do begin
- FRed:= GetRValue(Value);
- FGreen:= GetGValue(Value);
- FBlue:= GetBValue(Value);
- end;
- end;
- function TColorRec.GetHue: Double;
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- Result:= H;
- end;
- function TColorRec.GetSaturation: Double;
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- Result:= S;
- end;
- function TColorRec.GetBrightness: Double;
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- Result:= V;
- end;
- function TColorRec.GetCyan: Integer;
- begin
- Result:= GetCValue(RGB(FRed, FGreen, FBlue));
- end;
- function TColorRec.GetMagenta: Integer;
- begin
- Result:= GetMValue(RGB(FRed, FGreen, FBlue));
- end;
- function TColorRec.GetYellow: Integer;
- begin
- Result:= GetYValue(RGB(FRed, FGreen, FBlue));
- end;
- function TColorRec.GetBlack: Integer;
- begin
- Result:= GetKValue(RGB(FRed, FGreen, FBlue));
- end;
- procedure TColorRec.SetBrightness(const Value: Double);
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- V:= Value;
- HSVToRGB(H, S, V, FRed, FGreen, FBlue);
- end;
- procedure TColorRec.SetHue(const Value: Double);
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- H:= Value;
- HSVToRGB(H, S, V, FRed, FGreen, FBlue);
- end;
- procedure TColorRec.SetSaturation(const Value: Double);
- var
- H, S, V: Double;
- begin
- RGBToHSV(FRed, FGreen, FBlue, H, S, V);
- S:= Value;
- HSVToRGB(H, S, V, FRed, FGreen, FBlue);
- end;
- procedure TColorRec.SetCyan(const Value: Integer);
- begin
- Self:= CMYK(Value, Magenta, Yellow, Black);
- end;
- procedure TColorRec.SetMagenta(const Value: Integer);
- begin
- Self:= CMYK(Cyan, Value, Yellow, Black);
- end;
- procedure TColorRec.SetYellow(const Value: Integer);
- begin
- Self:= CMYK(Cyan, Magenta, Value, Black);
- end;
- procedure TColorRec.SetBlack(const Value: Integer);
- begin
- Self:= CMYK(Cyan, Magenta, Yellow, Value);
- end;
Advertisement
Add Comment
Please, Sign In to add comment