码迷,mamicode.com
首页 > Windows程序 > 详细

delphi 颜色转换函数总结

时间:2017-01-20 11:01:32      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:begin   web   tco   byte   res   graphics   word   long   unit   

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.

 

delphi 颜色转换函数总结

标签:begin   web   tco   byte   res   graphics   word   long   unit   

原文地址:http://www.cnblogs.com/yzryc/p/6322037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!