📄 jvqfullcolorspaces.pas
字号:
for Index := Low(ColorValues) to High(ColorValues) do
if Value = ColorValues[Index].Value then
begin
Result := ColorValues[Index].Description;
Exit;
end;
for Index := Low(SysColorValues) to High(SysColorValues) do
if Value = SysColorValues[Index].Value then
begin
Result := SysColorValues[Index].Description;
Exit;
end;
Result := ColorToString(Value);
end;
{$IFDEF VCL}
function PrettyNameToColor(Value: string): TColor;
var
Index: Integer;
ColorResult: Integer;
begin
for Index := Low(ColorValues) to High(ColorValues) do
if CompareText(Value,ColorValues[Index].Description) = 0 then
begin
Result := ColorValues[Index].Value;
Exit;
end;
for Index := Low(SysColorValues) to High(SysColorValues) do
if CompareText(Value,SysColorValues[Index].Description) = 0 then
begin
Result := SysColorValues[Index].Value;
Exit;
end;
if IdentToColor(Value,ColorResult) then
Result := ColorResult
else
Result := clNone;
end;
{$ENDIF}
//=== { TJvColorSpace } ======================================================
constructor TJvColorSpace.Create(ColorID: TJvFullColorSpaceID);
begin
inherited Create;
if (ColorID >= csMIN) and (ColorID <= csMAX) then
FID := ColorID
else
raise EJvColorSpaceError.CreateResFmt(@RsEIllegalID, [Ord(ColorID)]);
end;
function TJvColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
begin
Result := (AColor and $00FFFFFF) or (ID shl 24);
end;
function TJvColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
begin
Result := AColor and $00FFFFFF;
end;
function TJvColorSpace.GetNumberOfColors: Cardinal;
begin
Result :=
(AxisMax[axIndex0] - AxisMin[axIndex0] + 1) *
(AxisMax[axIndex1] - AxisMin[axIndex1] + 1) *
(AxisMax[axIndex2] - AxisMin[axIndex2] + 1);
end;
function TJvColorSpace.GetAxisDefault(Index: TJvAxisIndex): Byte;
begin
Result := Low(Byte);
end;
function TJvColorSpace.GetAxisMax(Index: TJvAxisIndex): Byte;
begin
Result := High(Byte);
end;
function TJvColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := Low(Byte);
end;
function TJvColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
Result := RsEUnnamedAxis;
end;
function TJvColorSpace.GetName: string;
begin
Result := RsEUnnamedSpace;
end;
function TJvColorSpace.GetShortName: string;
begin
Result := RsEUCS;
end;
//=== { TJvRGBColorSpace } ===================================================
function TJvRGBColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
begin
Result := inherited ConvertFromColor(AColor);
end;
function TJvRGBColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
begin
Result := inherited ConvertToColor(AColor);
end;
function TJvRGBColorSpace.GetAxisDefault(Index: TJvAxisIndex): Byte;
begin
Result := 0;
end;
function TJvRGBColorSpace.GetAxisMax(Index: TJvAxisIndex): Byte;
begin
Result := RGB_MAX;
end;
function TJvRGBColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := RGB_MIN;
end;
function TJvRGBColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
case Index of
axIndex0:
Result := RsRGB_Red;
axIndex1:
Result := RsRGB_Green;
axIndex2:
Result := RsRGB_Blue;
else
Result := inherited GetAxisName(Index);
end;
end;
function TJvRGBColorSpace.GetName: string;
begin
Result := RsRGB_FullName;
end;
function TJvRGBColorSpace.GetShortName: string;
begin
Result := RsRGB_ShortName;
end;
//=== { TJvHLSColorSpace } ===================================================
function TJvHLSColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
var
Hue, Lightness, Saturation: Double;
Red, Green, Blue: Integer;
ColorMax, ColorMin, ColorDiff, ColorSum: Double;
RedDelta, GreenDelta, BlueDelta: Extended;
begin
SplitColorParts(AColor, Red, Green, Blue);
if Red > Green then
ColorMax := Red
else
ColorMax := Green;
if Blue > ColorMax then
ColorMax := Blue;
if Red < Green then
ColorMin := Red
else
ColorMin := Green;
if Blue < ColorMin then
ColorMin := Blue;
ColorDiff := ColorMax - ColorMin;
ColorSum := ColorMax + ColorMin;
Lightness := (ColorSum * HLS_MAX + RGB_MAX) / (2.0 * RGB_MAX);
if ColorMax = ColorMin then
AColor := (Round(Lightness) shl 8) or (2 * HLS_MAX div 3)
else
begin
if Lightness <= HLS_MAX_HALF then
Saturation := (ColorDiff * HLS_MAX + ColorSum / 2.0) / ColorSum
else
Saturation := (ColorDiff * HLS_MAX + ((2.0 * RGB_MAX - ColorMax - ColorMin) / 2.0)) /
(2.0 * RGB_MAX - ColorMax - ColorMin);
RedDelta := ((ColorMax - Red) * HLS_MAX_SIXTH + ColorDiff / 2.0) / ColorDiff;
GreenDelta := ((ColorMax - Green) * HLS_MAX_SIXTH + ColorDiff / 2.0) / ColorDiff;
BlueDelta := ((ColorMax - Blue) * HLS_MAX_SIXTH + ColorDiff / 2.0) / ColorDiff;
if Red = ColorMax then
Hue := BlueDelta - GreenDelta
else
if Green = ColorMax then
Hue := HLS_MAX_ONE_THIRD + RedDelta - BlueDelta
else
Hue := 2.0 * HLS_MAX_ONE_THIRD + GreenDelta - RedDelta;
if Hue < 0 then
Hue := Hue + HLS_MAX;
if Hue > HLS_MAX then
Hue := Hue - HLS_MAX;
AColor :=
JoinColorParts(Cardinal(Round(Hue)), Cardinal(Round(Lightness)), Cardinal(Round(Saturation)));
end;
Result := inherited ConvertFromColor(AColor);
end;
function TJvHLSColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
var
Red, Green, Blue: Double;
Magic1, Magic2: Double;
Hue, Lightness, Saturation: Integer;
function HueToRGB(Lightness, Saturation, Hue: Double): Integer;
var
ResultEx: Double;
begin
if Hue < 0 then
Hue := Hue + HLS_MAX;
if Hue > HLS_MAX then
Hue := Hue - HLS_MAX;
if Hue < HLS_MAX_SIXTH then
ResultEx := Lightness + ((Saturation - Lightness) * Hue + HLS_MAX_TWELVETH) / HLS_MAX_SIXTH
else
if Hue < HLS_MAX_HALF then
ResultEx := Saturation
else
if Hue < HLS_MAX_TWO_THIRDS then
ResultEx := Lightness + ((Saturation - Lightness) * (HLS_MAX_TWO_THIRDS - Hue) + HLS_MAX_TWELVETH) / HLS_MAX_SIXTH
else
ResultEx := Lightness;
Result := Round(ResultEx);
end;
function RoundColor(Value: Double): Integer;
begin
if Value > RGB_MAX then
Result := RGB_MAX
else
Result := Round(Value);
end;
begin
SplitColorParts(AColor, Hue, Lightness, Saturation);
if Saturation = 0 then
begin
Red := (Lightness * RGB_MAX) / HLS_MAX;
Green := Red;
Blue := Red;
end
else
begin
if Lightness <= HLS_MAX_HALF then
Magic2 := (Lightness * (HLS_MAX + Saturation) + HLS_MAX_HALF) / HLS_MAX
else
Magic2 := Lightness + Saturation - ((Lightness * Saturation) + HLS_MAX_HALF) / HLS_MAX;
Magic1 := 2 * Lightness - Magic2;
Red := (HueToRGB(Magic1, Magic2, Hue + HLS_MAX_ONE_THIRD) * RGB_MAX + HLS_MAX_HALF) / HLS_MAX;
Green := (HueToRGB(Magic1, Magic2, Hue) * RGB_MAX + HLS_MAX_HALF) / HLS_MAX;
Blue := (HueToRGB(Magic1, Magic2, Hue - HLS_MAX_ONE_THIRD) * RGB_MAX + HLS_MAX_HALF) / HLS_MAX;
end;
Result := inherited ConvertToColor(
JoinColorParts(RoundColor(Red), RoundColor(Green), RoundColor(Blue)));
end;
function TJvHLSColorSpace.GetAxisDefault(Index: TJvAxisIndex): Byte;
begin
Result := 120;
end;
function TJvHLSColorSpace.GetAxisMax(Index: TJvAxisIndex): Byte;
begin
Result := HLS_MAX;
end;
function TJvHLSColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := HLS_MIN;
end;
function TJvHLSColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
case Index of
axIndex0:
Result := RsHLS_Hue;
axIndex1:
Result := RsHLS_Lightness;
axIndex2:
Result := RsHLS_Saturation;
else
Result := inherited GetAxisName(Index);
end;
end;
function TJvHLSColorSpace.GetName: string;
begin
Result := RsHLS_FullName;
end;
function TJvHLSColorSpace.GetShortName: string;
begin
Result := RsHLS_ShortName;
end;
//=== { TJvCMYColorSpace } ===================================================
function TJvCMYColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
var
Red, Green, Blue: Integer;
Cyan, Magenta, Yellow: Integer;
begin
SplitColorParts(AColor, Red, Green, Blue);
Cyan := ((RGB_MAX - Red ) * (CMY_MAX-CMY_MIN+1) div (RGB_MAX-RGB_MIN+1)) + CMY_MIN;
Magenta := ((RGB_MAX - Green) * (CMY_MAX-CMY_MIN+1) div (RGB_MAX-RGB_MIN+1)) + CMY_MIN;
Yellow := ((RGB_MAX - Blue ) * (CMY_MAX-CMY_MIN+1) div (RGB_MAX-RGB_MIN+1)) + CMY_MIN;
Result := inherited ConvertFromColor(JoinColorParts(Cyan, Magenta, Yellow));
end;
function TJvCMYColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
var
Cyan, Magenta, Yellow: Integer;
Red, Green, Blue: Integer;
begin
SplitColorParts(AColor, Cyan, Magenta, Yellow);
Red := ((CMY_MAX - Cyan ) * (RGB_MAX-RGB_MIN+1) div (CMY_MAX-CMY_MIN+1)) + RGB_MIN;
Green := ((CMY_MAX - Magenta) * (RGB_MAX-RGB_MIN+1) div (CMY_MAX-CMY_MIN+1)) + RGB_MIN;
Blue := ((CMY_MAX - Yellow ) * (RGB_MAX-RGB_MIN+1) div (CMY_MAX-CMY_MIN+1)) + RGB_MIN;
Result := inherited ConvertToColor(JoinColorParts(Red, Green, Blue));
end;
function TJvCMYColorSpace.GetAxisDefault(Index: TJvAxisIndex): Byte;
begin
Result := 255;
end;
function TJvCMYColorSpace.GetAxisMax(Index: TJvAxisIndex): Byte;
begin
Result := CMY_MAX;
end;
function TJvCMYColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := CMY_MIN;
end;
function TJvCMYColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
case Index of
axIndex0:
Result := RsCMY_Cyan;
axIndex1:
Result := RsCMY_Magenta;
axIndex2:
Result := RsCMY_Yellow;
else
Result := inherited GetAxisName(Index);
end;
end;
function TJvCMYColorSpace.GetName: string;
begin
Result := RsCMY_FullName;
end;
function TJvCMYColorSpace.GetShortName: string;
begin
Result := RsCMY_ShortName;
end;
//=== { TJvYUVColorSpace } ===================================================
function TJvYUVColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
var
Y, U, V: Integer;
Red, Green, Blue: Integer;
begin
SplitColorParts(AColor, Red, Green, Blue);
Y := Round(0.257*Red + 0.504*Green + 0.098*Blue) + 16;
V := Round(0.439*Red - 0.368*Green - 0.071*Blue) + 128;
U := Round(-0.148*Red - 0.291*Green + 0.439*Blue) + 128;
Y := EnsureRange(Y, YUV_MIN, YUV_MAX);
U := EnsureRange(U, YUV_MIN, YUV_MAX);
V := EnsureRange(V, YUV_MIN, YUV_MAX);
Result := inherited ConvertFromColor(JoinColorParts(Y, U, V));
end;
function TJvYUVColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
var
Red, Green, Blue: Integer;
Y, U, V: Integer;
begin
SplitColorParts(AColor, Y, U, V);
Y := Y - 16;
U := U - 128;
V := V - 128;
Red := Round(1.164*Y - 0.002*U + 1.596*V);
Green := Round(1.164*Y - 0.391*U - 0.813*V);
Blue := Round(1.164*Y + 2.018*U - 0.001*V);
Red := EnsureRange(Red , RGB_MIN, RGB_MAX);
Green := EnsureRange(Green, RGB_MIN, RGB_MAX);
Blue := EnsureRange(Blue, RGB_MIN, RGB_MAX);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -