在DELPHI下, 把 字符串 "#FF00FF" (web页中的颜色) 怎么转换成 TColor 呢?
请大家帮忙想想~~, 谢谢,, ,  如果只是一两个函数就可解决话,,我想我得撞墙了 :)

解决方案 »

  1.   

    var r:TColor;...
    r :=RGB(#ff,#00,#ff);
      

  2.   

    找到一个:
    语法: 
    ------- 
    myTcolor:=rgbtocolor(redvalue,greenvalue,bluevalue); 
    例子: 
    -------- 
    form1.color:=rgbtocolor(255,0,0); 
    函数: 
    --------- 
    function RGBToColor(R,G,B:Byte): TColor; 
    begin 
      Result:=B Shl 16 Or 
              G Shl 8  Or 
              R; 
    end; 
     
      

  3.   

    请注意Help中关于TColor的介绍
    If you specify TColor as a specific 4-byte hexadecimal number instead of using the constants defined in the Graphics unit, the low three bytes represent RGB color intensities for blue, green, and red, respectively. The value $00FF0000 (Delphi) or 0x00FF0000 (C++) represents full-intensity, pure blue, $0000FF00 (Delphi) or 0x0000FF00 (C++) is pure green, and $000000FF (Delphi) or 0x000000FF (C++) is pure red. $00000000 (Delphi) or 0x00000000 (C++) is black and $00FFFFFF (Delphi) or 0x00FFFFFF (C++) is white.If the highest-order byte is zero, the color obtained is the closest matching color in the system palette. If the highest-order byte is one ($01 or 0x01), the color obtained is the closest matching color in the currently realized palette. If the highest-order byte is two ($02 or 0x02), the value is matched with the nearest color in the logical palette of the current device context.  TColor定义为type TColor = -$7FFFFFFF-1..$7FFFFFFF;
      也就是4个Byte。其中低三Byte就是你说的"#FF00FF",也就是RGB表示。关于第4Byte的定义,请看上面的文档。
      所以,一般来说,你可以:
      直接把Web Color字符串,取最后六个字符(Copy函数),然后添上一个$00(+),就可以了
      

  4.   

    (*//
    声明:
      本人保证所提供的方法是所知的最好方法
      解答问题纯属公益性质
      所以请不要向我追问
      如果有时间自会关注后续问题
    //*)///////Begin Source
    function ColorToHtml(mColor: TColor): string;
    begin
      mColor := ColorToRGB(mColor);
      Result := Format('#%.2x%.2x%.2x',
        [GetRValue(mColor), GetGValue(mColor), GetBValue(mColor)]);
    end; { ColorToHtml }function HtmlToColor(mHtml: string): TColor;
    begin
      ///////Begin 清除无效字符
      mHtml := StringReplace(mHtml, '#', '', [rfReplaceAll]);
      mHtml := StringReplace(mHtml, ' ', '', [rfReplaceAll]);
      mHtml := StringReplace(mHtml, '"', '', [rfReplaceAll]);
      ///////End 清除无效字符  Result := RGB(StrToIntDef('$' + Copy(mHtml, 1, 2), 0),
        StrToIntDef('$' + Copy(mHtml, 3, 2), 0),
        StrToIntDef('$' + Copy(mHtml, 5, 2), 0));
    end; { HtmlToColor }
    ///////End Source///////Begin Demo
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Color := HtmlToColor(Edit1.Text);
      Caption := ColorToHtml(Color);
    end;
    ///////End Demo
      

  5.   

    楼上那仁兄, 你也太自夸了,“声明:
      本人保证所提供的方法是所知的最好方法
      解答问题纯属公益性质
      所以请不要向我追问
      如果有时间自会关注后续问题

    ----------------恶心,不过,还是谢谢你,你的StrToIntDef是整个的关键,
    但是,我不得不说
    你的代码写得太,,太,,,  臃肿了,,
      

  6.   

    stringtocolor 或者 colortostring 不能用吗?
      

  7.   

    to liuchcn: 当然也可以,但位置要变换一下。RGB 在Delphi中是 BGR!