delphi 색상 변환 함수 요약

11477 단어
unit UColor;

interface

uses windows, sysutils, classes, graphics;

function HexToInt(Hexa: String): LongWord;

function ColorToString(color: TColor): String;


function WebColorToDelphiTColor(webcolor: String): TColor;
function HexToTColor(sHtmlColor: String): TColor;

function HexIntColorToHtmlColor(c: Integer): String;
function TColorToWebColor(DColor: TColor): String;
function HexStrColorToHtmlColor(s: String): String;






implementation

function HexToInt(Hexa: String): LongWord;
const
  ValoresHexa: array['A'..'F'] of Integer = (10, 11, 12, 13, 14, 15);
var
  nDecimal: LongWord;
  nIndex: Byte;
begin
  nDecimal := 0;
  Hexa := Uppercase(Hexa);
  for nIndex := Length(Hexa) downto 1 do
    if Hexa[nIndex] in ['0'..'9'] then
      nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
        Trunc(Exp((Length(Hexa) - nIndex) * ln(16)))
  else
    nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
      Trunc(Exp((Length(Hexa) - nIndex) * ln(16)));
  Result := nDecimal;
end;

function ColorToString(color: TColor): String;
var
  r, g, b: Byte;
begin
  r := GetRValue(color);
  g := GetgValue(color);
  b := GetbValue(color);
  Result := '$' + IntToHex(TColor(RGB(r, g, b)), 8);
end;


function WebColorToDelphiTColor(webcolor: String): TColor;
var
  a: array [0..3] of Byte;
  b: array[0..3] of Byte;
begin
{
      rgb  ,   6 16         
      RGB            , TCOLOR      ,
        
       RGB : F1F2FE
       Tcolor: $00FEF2F1
}
  Integer(a) := HexToInt(webcolor);
  if a[3] = 0 then
  begin
    b[0] := a[2];
    b[1] := a[1];
    b[2] := a[0];
    b[3] := 0;
  end
  else
  begin
    b[0] := a[3];
    b[1] := a[2];
    b[2] := a[1];
    b[3] := a[0];
  end;
  Result := TColor(b);
end;


function HexToTColor(sHtmlColor: String): TColor;
begin
  //    WebColorToDelphiTColor      
  if pos('#', sHtmlColor) = 1 then
    sHtmlColor := copy(sHtmlColor, 2, length(sHtmlColor));

  Result :=
    RGB(StrToInt(#36 + Copy(sHtmlColor, 1, 2)),
    StrToInt(#36 + Copy(sHtmlColor, 3, 2)), StrToInt(#36 + Copy(sHtmlColor, 5, 2)));
end;


function HexIntColorToHtmlColor(c: Integer): String;
var
  R, G, B: Byte;
begin
  R := c and $FF;
  G := (c shr 8) and $FF;
  B := (c shr 16) and $FF;
  Result := #35 + Format('%.2x%.2x%.2x', [R, G, B]);
end;

{            Html   }
function HexStrColorToHtmlColor(s: String): String;
var
  i: Integer;
  R, G, B: Byte;
begin
  i := StrToInt(s);
  R := i and $FF;
  G := (i shr 8) and $FF;
  B := (i shr 16) and $FF;
  Result := #35 + Format('%.2x%.2x%.2x', [R, G, B]);
end;

function TColorToWebColor(DColor: TColor): String;
var
  tmpRGB: TColorRef;
begin
  tmpRGB := ColorToRGB(DColor);
  Result := Format('#%.2x%.2x%.2x', [GetRValue(tmpRGB),
    GetGValue(tmpRGB), GetBValue(tmpRGB)]);
end;




end.

 
전재 대상:https://www.cnblogs.com/yzryc/p/6322037.html

좋은 웹페이지 즐겨찾기