📄 jvqfullcolorspaces.pas
字号:
begin
Result := XYZ_MAX;
end;
function TJvXYZColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := XYZ_MIN;
end;
function TJvXYZColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
case Index of
axIndex0:
Result := RsXYZ_X;
axIndex1:
Result := RsXYZ_Y;
axIndex2:
Result := RsXYZ_Z;
else
Result := inherited GetAxisName(Index);
end;
end;
function TJvXYZColorSpace.GetName: string;
begin
Result := RsXYZ_FullName;
end;
function TJvXYZColorSpace.GetShortName: string;
begin
Result := RsXYZ_ShortName;
end;
//=== { TJvLABColorSpace } ===================================================
function TJvLABColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
var
X, Y, Z: Extended;
L, A, B: Integer;
Red, Green, Blue: Integer;
function Calc(Value: Extended): Extended;
begin
if Value > 0.008856 then
Result := Power(Value, 1.0 / 3.0)
else
Result := 7.7787 * Value + (16.0 / 116.0);
end;
begin
SplitColorParts(AColor, Red, Green, Blue);
X := (0.618*Red + 0.177*Green + 0.205*Blue) / XYZ_MAX;
Y := (0.299*Red + 0.587*Green + 0.114*Blue) / XYZ_MAX;
Z := ( 0.056*Green + 0.944*Blue) / XYZ_MAX;
X := EnsureRange(X, 0.0, 1.0);
Y := EnsureRange(Y, 0.0, 1.0);
Z := EnsureRange(Z, 0.0, 1.0);
if Y > 0.008856 then
L := Round(116.0 * Power(Y, 1.0 / 3.0) - 16.0)
else
L := Round(903.3 * Y);
A := Round(500.0 *(Calc(X) - Calc(Y)))+128;
B := Round(200.0 *(Calc(Y) - Calc(Z)))+128;
L := EnsureRange(L, LAB_MIN, LAB_MAX);
A := EnsureRange(A, LAB_MIN, LAB_MAX);
B := EnsureRange(B, LAB_MIN, LAB_MAX);
Result := inherited ConvertFromColor(JoinColorParts(L, A, B));
end;
function TJvLABColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
var
Red, Green, Blue: Integer;
X, Y, Z: Extended;
L, A, B: Integer;
function Calc(Value: Extended): Extended;
begin
if Value > 0.207 then
Result := Power(Value, 3.0)
else
Result := ((116.0 * Value) - 16.0) / 903.3;
end;
begin
SplitColorParts(AColor, L, A, B);
if L > 8 then
Y := XYZ_MAX * Power((L + 16.0) / 116.0, 3.0)
else
Y := (XYZ_MAX * L) / 903.3;
X := XYZ_MAX * Calc(((A-128) / 500.0) + ((L + 16.0) / 116.0));
Z := XYZ_MAX * Calc(((L + 16.0) / 116.0) - ((B-128) / 200.0));
X := EnsureRange(X, XYZ_MIN, XYZ_MAX);
Y := EnsureRange(Y, XYZ_MIN, XYZ_MAX);
Z := EnsureRange(Z, XYZ_MIN, XYZ_MAX);
Red := Round( 1.876*X - 0.533*Y - 0.343*Z);
Green := Round(-0.967*X + 1.998*Y - 0.031*Z);
Blue := Round( 0.057*X - 0.118*Y + 1.061*Z);
Red := EnsureRange(Red , RGB_MIN, RGB_MAX);
Green := EnsureRange(Green, RGB_MIN, RGB_MAX);
Blue := EnsureRange(Blue, RGB_MIN, RGB_MAX);
Result := inherited ConvertToColor(JoinColorParts(Red, Green, Blue));
end;
function TJvLABColorSpace.GetAxisDefault(Index: TJvAxisIndex): Byte;
begin
Result := 50;
end;
function TJvLABColorSpace.GetAxisMax(Index: TJvAxisIndex): Byte;
begin
Result := LAB_MAX;
end;
function TJvLABColorSpace.GetAxisMin(Index: TJvAxisIndex): Byte;
begin
Result := LAB_MIN;
end;
function TJvLABColorSpace.GetAxisName(Index: TJvAxisIndex): string;
begin
case Index of
axIndex0:
Result := RsLAB_L;
axIndex1:
Result := RsLAB_A;
axIndex2:
Result := RsLAB_B;
else
Result := inherited GetAxisName(Index);
end;
end;
function TJvLABColorSpace.GetName: string;
begin
Result := RsLAB_FullName;
end;
function TJvLABColorSpace.GetShortName: string;
begin
Result := RsLAB_ShortName;
end;
//=== { TJvDEFColorSpace } ===================================================
constructor TJvDEFColorSpace.Create(ColorID: TJvFullColorSpaceID);
begin
inherited Create(ColorID);
FDelphiColors := TStringList.Create;
// ignore duplicates
FDelphiColors.Duplicates:=dupIgnore;
GetColorValues(GetColorValuesCallBack);
AddDelphiColor(clNone);
end;
destructor TJvDEFColorSpace.Destroy;
begin
FDelphiColors.Free;
inherited Destroy;
end;
procedure TJvDEFColorSpace.GetColorValuesCallBack(const S: String);
var
AColor: TColor;
begin
AColor := StringToColor(S);
AddCustomColor(AColor,Copy(S,3,Length(S)-2),ColorToPrettyName(AColor));
end;
procedure TJvDEFColorSpace.AddDelphiColor(Value: TColor);
begin
AddCustomColor(Value,ColorToString(Value),ColorToPrettyName(Value));
end;
procedure TJvDEFColorSpace.AddCustomColor(AColor: TColor; ShortName,
PrettyName: string);
begin
if FDelphiColors.IndexOfObject(TObject(AColor)) = -1 then
begin
FDelphiColors.Values[ShortName] := PrettyName;
FDelphiColors.Objects[FDelphiColors.IndexOfName(ShortName)] := TObject(AColor);
end;
end;
function TJvDEFColorSpace.ConvertFromColor(AColor: TColor): TJvFullColor;
var
I: Integer;
NewColor: TColor;
begin
NewColor := clNone;
for I := 0 to FDelphiColors.Count - 1 do
if AColor = TColor(FDelphiColors.Objects[I]) then
begin
NewColor := AColor;
Break;
end;
Result := inherited ConvertFromColor(NewColor);
if NewColor = clNone then
// mark it as clNone
Result := Result or JvSpecialFullColorMask
else
if NewColor = clDefault then
// mark it as clDefault
Result := Result or JvSpecialFullColorMask
else
if (NewColor and JvSystemColorMask) = JvSystemColorMask then
// mark it as predefined color
Result := Result or JvSystemFullColorMask
else
if (NewColor and JvSystemColorMask) = 0 then
Result := ColorSpaceManager.ColorSpace[csRGB].ConvertFromColor(NewColor)
// should never happend because there should be no way ...
else
raise EJvColorSpaceError.CreateResFmt(@RsEInconvertibleColor, [Cardinal(NewColor)]);
end;
function TJvDEFColorSpace.ConvertToColor(AColor: TJvFullColor): TColor;
begin
Result := inherited ConvertToColor(AColor);
case AColor and JvSubFullColorMask of
JvSystemFullColorMask:
Result := Cardinal(Result) or JvSystemColorMask;
JvSpecialFullColorMask:
begin
if Result = (clNone and $FFFFFF) then
Result := clNone
else
if Result = (clDefault and $FFFFFF) then
Result := clDefault
else
raise EJvColorSpaceError.CreateResFmt(@RsEInconvertibleColor, [Cardinal(AColor)]);
end;
else
raise EJvColorSpaceError.CreateResFmt(@RsEInconvertibleColor, [Cardinal(AColor)]);
end;
end;
function TJvDEFColorSpace.GetColorName(Index: Integer): string;
begin
if (Index >= 0) and (Index < FDelphiColors.Count) then
Result := FDelphiColors.Names[Index]
else
Result := '';
end;
function TJvDEFColorSpace.GetPrettyName(Index: Integer): string;
begin
Result := FDelphiColors.Values[FDelphiColors.Names[Index]];
end;
function TJvDEFColorSpace.GetColorValue(Index: Integer): TColor;
begin
if (Index >= 0) and (Index < FDelphiColors.Count) then
Result := TColor(FDelphiColors.Objects[Index])
else
Result := clNone;
end;
function TJvDEFColorSpace.GetName: string;
begin
Result := RsDEF_FullName;
end;
function TJvDEFColorSpace.GetShortName: string;
begin
Result := RsDEF_ShortName;
end;
function TJvDEFColorSpace.GetNumberOfColors: Cardinal;
begin
Result := FDelphiColors.Count;
end;
//=== { TJvColorSpaceManager } ===============================================
constructor TJvColorSpaceManager.Create;
begin
inherited Create;
FColorSpaceList := TList.Create;
end;
destructor TJvColorSpaceManager.Destroy;
var
Index: Integer;
begin
for Index := 0 to FColorSpaceList.Count - 1 do
TJvColorSpace(FColorSpaceList.Items[Index]).Free;
FColorSpaceList.Free;
inherited Destroy;
end;
function TJvColorSpaceManager.ConvertToID(AColor: TJvFullColor;
DestID: TJvFullColorSpaceID): TJvFullColor;
var
SourceID: TJvFullColorSpaceID;
Color: TColor;
begin
SourceID := GetColorSpaceID(AColor);
if SourceID = DestID then
Result := AColor
else
begin
Color := ColorToRGB(ColorSpace[SourceID].ConvertToColor(AColor));
Result := ColorSpace[DestID].ConvertFromColor(Color);
end;
end;
function TJvColorSpaceManager.ConvertToColor(AColor: TJvFullColor): TColor;
begin
Result := ColorSpace[GetColorSpaceID(AColor)].ConvertToColor(AColor);
end;
function TJvColorSpaceManager.ConvertFromColor(AColor: TColor): TJvFullColor;
var
MaskedColor:Cardinal;
begin
MaskedColor := Cardinal(AColor) and JvSystemColorMask;
if (AColor = clNone) or (AColor = clDefault) or
(MaskedColor = JvSystemColorMask) then
Result := ColorSpace[csDEF].ConvertFromColor(AColor)
else
if MaskedColor = 0 then
Result := ColorSpace[csRGB].ConvertFromColor(AColor)
else
raise EJvColorSpaceError.CreateResFmt(@RsEInconvertibleColor, [Cardinal(AColor)]);
end;
function TJvColorSpaceManager.GetColorSpaceID(AColor: TJvFullColor): TJvFullColorSpaceID;
var
I: Integer;
begin
Result := TJvFullColorSpaceID(AColor shr 24) and csID_MASK;
for I := 0 to Count - 1 do
if ColorSpaceByIndex[I].ID = Result then
Exit;
raise EJvColorSpaceError.CreateResFmt(@RsEIllegalID, [Ord(Result)]);
end;
function TJvColorSpaceManager.GetColorSpace(ID: TJvFullColorSpaceID): TJvColorSpace;
var
I: Integer;
begin
Result := nil;
for I := 0 to FColorSpaceList.Count - 1 do
begin
Result := TJvColorSpace(FColorSpaceList.Items[I]);
if Result.ID = ID then
Break;
end;
if Result = nil then
raise EJvColorSpaceError.CreateResFmt(@RsECSNotFound, [Ord(ID)]);
end;
function TJvColorSpaceManager.GetCount: Integer;
begin
Result := FColorSpaceList.Count;
end;
function TJvColorSpaceManager.GetColorSpaceByIndex(Index: Integer): TJvColorSpace;
begin
Result := TJvColorSpace(FColorSpaceList.Items[Index]);
end;
procedure TJvColorSpaceManager.RegisterColorSpace(NewColorSpace: TJvColorSpace);
var
Index: Integer;
CS: TJvColorSpace;
begin
for Index := 0 to FColorSpaceList.Count - 1 do
begin
CS := TJvColorSpace(FColorSpaceList.Items[Index]);
if CS.ID = NewColorSpace.ID then
raise EJvColorSpaceError.CreateFmt(RsECSAlreadyExists, [CS.ID, CS.Name]);
end;
FColorSpaceList.Add(Pointer(NewColorSpace));
end;
procedure TJvColorSpaceManager.UnRegisterColorSpace(AColorSpace: TJvColorSpace);
begin
// maybe more than one instance of one class
while FColorSpaceList.Remove(AColorSpace) >= 0 do
;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvQFullColorSpaces.pas,v $';
Revision: '$Revision: 1.2 $';
Date: '$Date: 2005/02/06 14:06:12 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
initialization
{$IFDEF UNITVERSIONING}
RegisterUnitVersion(HInstance, UnitVersioning);
{$ENDIF UNITVERSIONING}
finalization
FreeAndNil(GlobalColorSpaceManager);
{$IFDEF UNITVERSIONING}
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -