颜色拾取器
提示:这里可以添加技术概要
核心源码
var
Spectroscope: TSpectroscope;
R, G, B, BaseR, BaseB, BaseG: string;
H, S, L: double;
sTemp: string;
down: boolean;
Ratio: real;
CustomPaletteWidth, CustomPaletteHeight: longint;
TheColor: TColor;
//used by loadfromclipboardformat function
ImageGraphicClass: TGraphicClass;
ImageFormat: word;
ImagePalette: HPALETTE;
const
crEyedrop = 5;
implementation
uses Hslutils;
{ KaTeX parse error: Expected 'EOF', got '}' at position 8: R *.DFM}̲ { R Cursors.dcr}
//-----------------------------------------------------
// MATH INTERNAL LIBRARY
//-----------------------------------------------------
function isHex(s: string): boolean;
var
i: integer;
chars: string;
begin
chars := ‘0123456789ABCDEFabcdef#’;
isHex := true;
if (length(s) > 7) then
isHex := false;
for i := 1 to length(s) do
begin
if (pos(s[i], chars) = 0) then
isHex := false;
end;
end;
function isDec(s: string): boolean;
var
i: integer;
chars: string;
begin
chars := ‘0123456789’;
isDec := true;
for i := 1 to length(s) do
begin
if (pos(s[i], chars) = 0) then
isDec := false;
end;
end;
function pow(n, e: longint): integer;
var
i: longint;
temp: longint;
begin
if e = 0 then
begin
pow := 1;
exit;
end;
temp := 1;
for i := 1 to e do
temp := temp * n;
pow := temp;
end;
function getHexCharValue(HexChar: char): byte;
begin
case HexChar of
‘0’: getHexCharValue := 0;
‘1’: getHexCharValue := 1;
‘2’: getHexCharValue := 2;
‘3’: getHexCharValue := 3;
‘4’: getHexCharValue := 4;
‘5’: getHexCharValue := 5;
‘6’: getHexCharValue := 6;
‘7’: getHexCharValue := 7;
‘8’: getHexCharValue := 8;
‘9’: getHexCharValue := 9;
‘A’: getHexCharValue := 10;
‘B’: getHexCharValue := 11;
‘C’: getHexCharValue := 12;
‘D’: getHexCharValue := 13;
‘E’: getHexCharValue := 14;
‘F’: getHexCharValue := 15;
else
getHexCharValue := 0;
end;
end;
function HexToInt(number: string): Integer;
var
i, temp: Integer;
e: integer;
begin
e := 0;
temp := 0;
for i := length(number) downto 1 do
begin
temp := temp + (GetHexCharValue(number[i]) * pow(16, e));
inc(e);
end;
HexToInt := temp;
end;
//-----------------------------------------------------
//
//-----------------------------------------------------
//-----------------------------------------------------
// INTERNAL SET FUNCTIONS
//-----------------------------------------------------
procedure TSpectroscope.setRGBfromBGR(BGR: string); //返回16进制的R,G,B三个分量
begin
B := copy(BGR, 1, 2);
//从第一个字符开始取两个字符 //$2345f4
G := copy(BGR, 3, 2); //从第三个字符开始,取两个字符
R := copy(BGR, 5, 2); //从第五个字符开始
end;
procedure TSpectroscope.SetRGBfromDecEdits();
begin
R := IntToHex(StrToInt(EditR.Text), 2);
g := IntToHex(StrToInt(EditG.Text), 2);
B := IntToHex(StrToInt(EditB.Text), 2);
end;
procedure TSpectroscope.SetRGBfromHSLEdits();
begin
setRGBfromBGR(inttohex(ColorToRGB(HSLtoRGB((strtofloat(EditH.text) / 100),
(strtofloat(EditS.text) / 100), (strtofloat(EditL.text) / 100))), 6));
end;
procedure TSpectroscope.SetRGBfromHexEdit();
begin
R := Copy(EditHex.text, 2, 2);
G := Copy(EditHex.text, 4, 2);
B := Copy(EditHex.text, 6, 2);
end;
procedure TSpectroscope.UpdateAllColors;
begin
EditHex.text := ‘#’ + R + G + B;
EditR.Text := IntToStr(HexToInt®);
EditG.Text := IntToStr(HexToInt(G));
EditB.Text := IntToStr(HexToInt(B));
TheColor := strtoint(‘$’ + B + G + R);
RGBtoHSL(TheColor, H, S, L);
EditH.Text := inttostr(round(H * 100));
EditS.Text := inttostr(round(S * 100));
EditL.Text := inttostr(round(L * 100));
ColorPanel.Color := TheColor;
end;
procedure TSpectroscope.UpdatePalettes;
begin
RGBToHSL(TheColor, H, S, L);
GradFill3Colors(HSLToRGB(H, 1, L), TheColor, HSLToRGB(H, 0, L),
ImgSatPalette);
CursorSat.Left := 126;
CursorSat.Top := 126;
GradFill3Colors($FFFFFF, TheColor, $000000, ImgBrightPalette);
CursorBright.Left := 126;
CursorBright.Top := 126;
end;
//-----------------------------------------------------
//
//-----------------------------------------------------
//-----------------------------------------------------
// PAINTING FUNCTIONS
//-----------------------------------------------------
procedure TSpectroscope.DrawSlide(X: integer; TheBitmap: TImage);
begin
TheBitmap.Canvas.Brush.Color := $FFFFFF;
TheBitmap.Canvas.Pen.Color := $FFFFFF;
TheBitmap.Canvas.Rectangle(0, 0, TheBitmap.Width, TheBitmap.Height);
TheBitmap.Canvas.Brush.Color := clActiveCaption;
TheBitmap.Canvas.Pen.Color := clActiveCaption;
TheBitmap.Canvas.Rectangle(0, 0, X, TheBitmap.Height);
end;
procedure TSpectroscope.GradFill3Colors(Clr1, Clr2, Clr3: TColor; TheBitmap:
TImage);
var
RGBFrom: array[0…2] of Byte; { from RGB values }
RGBDiff: array[0…2] of integer; { difference of from/to RGB values }
ColorBand: TRect; { color band rectangular coordinates }
I: Integer; { color band index }
R: Byte; { a color band’s R value }
G: Byte; { a color band’s G value }
B: Byte; { a color band’s B value }
begin
{GRADIENT 1}
{ extract from RGB values}
RGBFrom[0] := GetRValue(ColorToRGB(Clr1));
RGBFrom[1] := GetGValue(ColorToRGB(Clr1));
RGBFrom[2] := GetBValue(ColorToRGB(Clr1));
{ calculate difference of from and to RGB values}
RGBDiff[0] := GetRValue(ColorToRGB(Clr2)) - RGBFrom[0];
RGBDiff[1] := GetGValue(ColorToRGB(Clr2)) - RGBFrom[1];
RGBDiff[2] := GetBValue(ColorToRGB(Clr2)) - RGBFrom[2];
{ set pen sytle and mode}
TheBitmap.Canvas.Pen.Style := psSolid;
TheBitmap.Canvas.Pen.Mode := pmCopy;
{ set color band’s left and right coordinates}
ColorBand.Left := 0;
ColorBand.Right := TheBitmap.Width;
for I := 0 to $FF do
begin
{ calculate color band’s top and bottom coordinates}
ColorBand.Top := MulDiv(I, TheBitmap.Height div 2, $100);
ColorBand.Bottom := MulDiv(I + 1, TheBitmap.Height div 2, $100);
{ calculate color band color}
R := RGBFrom[0] + Mul