2011-04-21 82 views

回答

6

Delphi中有一個叫GraphUtil其中有一個叫SortColorArray

procedure SortColorArray(ColorArray: TColorArray; L, R: Integer; 
    SortType: TColorArraySortType; Reverse: Boolean = False); 

此功能可以按色調,飽和度,亮度,紅,綠,藍的顏色列表功能單元。

,你可以通過這種方式

var 
ColorList : TColorArray; 
i   : Integer; 
begin 

    SetLength(ColorList,WebNamedColorsCount); 
    //fill the list the colors in this case using webcolors 
    for i := 0 to WebNamedColorsCount - 1 do 
    begin 
    ColorList[i].Value:=WebNamedColors[i].Value; 
    ColorList[i].Name :=''; 
    end; 
    //sort the colors by HUE 
    SortColorArray(ColorList,0,WebNamedColorsCount-1,stHue,False); 

    //do your stuff here 

end; 
+0

+1我只是把這些寫功能useit!即使我自己不使用它們,我也知道它們存在! – 2011-04-21 00:45:22

+1

+1。太好了!我從未注意到這個功能。它是什麼時候添加的(或者它一直在那裏)? – 2011-04-21 00:46:49

+0

@Andreas,幾個月前,我發現了這個功能,而我寫了一個應用程序來改變一些顏色的HUE。只是我想知道爲什麼你更喜歡編寫你自己的函數而不使用它?是因爲表現? – RRUZ 2011-04-21 00:50:22

1

我做

type 
    TRGB = record 
    rgbRed,    // Red intensity in [0, 1] 
    rgbGreen,   // Green intensity in [0, 1] 
    rgbBlue: double;  // Blue intensity in [0, 1] 
    end; 

    THSV = record 
    hsvHue,    // Hue angle in [0, 360] 
    hsvSaturation,  // Saturation in [0, 1] 
    hsvValue: double; // Value in [0, 1] 
    end; 

function MaxComponent(const Color: TRGB): real; 
begin 
    with Color do 
    result := max(max(rgbRed, rgbGreen), rgbBlue); 
end; 

function MinComponent(const Color: TRGB): real; 
begin 
    with Color do 
    result := min(min(rgbRed, rgbGreen), rgbBlue); 
end; 

function Fix360(const a: Real): real; 
begin 
    result := a; 
    if result > 360 then 
    while result > 360 do 
     result := result - 360 
    else if result < 0 then 
    while result < 0 do 
     result := result + 360; 
end; 


function RGBToHSV(const Color: TRGB): THSV; 
var 
    cmax, cmin, cdiff: real; 
begin 
    cmax := MaxComponent(Color); 
    cmin := MinComponent(Color); 
    cdiff := cmax - cmin; 

    with Color, result do 
    begin 

    // Hue 
    if cmax = cmin then 
     hsvHue := 0 
    else if cmax = rgbRed then 
     hsvHue := (60 * (rgbGreen - rgbBlue)/cdiff) 
    else if cmax = rgbGreen then 
     hsvHue := (60 * (rgbBlue - rgbRed)/cdiff) + 120 
    else 
     hsvHue := (60 * (rgbRed - rgbGreen)/cdiff) + 240; 

    hsvHue := Fix360(hsvHue); 

    // Saturation 
    if cmax = 0 then 
     hsvSaturation := 0 
    else 
     hsvSaturation := 1 - cmin/cmax; 

    // Value 
    hsvValue := cmax; 

    end; 

end; 

function HSVToRGB(const Color: THSV): TRGB; 
var 
    hi: integer; 
    f, q, p, t: real; 
begin 

    with Color do 
    begin 

    hi := floor(hsvHue/60) mod 6; 
    f := hsvHue/60 - floor(hsvHue/60); 
    p := hsvValue * (1 - hsvSaturation); 
    q := hsvValue * (1 - f * hsvSaturation); 
    t := hsvValue * (1 - (1 - f) * hsvSaturation); 

    case hi of 
     0: result := RGB(hsvValue, t, p); 
     1: result := RGB(q, hsvValue, p); 
     2: result := RGB(p, hsvValue, t); 
     3: result := RGB(p, q, hsvValue); 
     4: result := RGB(t, p, hsvValue); 
     5: result := RGB(hsvValue, p, q); 
    end; 

    end; 

end; 

到RGB和HSV之間的轉換。現在你可以很容易地得到任何顏色的色調,排序應該是微不足道的。

更新

如何獲取從TColor一個TRGB?只要做到

function PascalColorToRGB(Color: TColor): TRGB; 
begin 
    Color := GetSysColor(Color); 
    with result do 
    begin 
    rgbRed := GetRValue(Color)/255; 
    rgbGreen := GetGValue(Color)/255; 
    rgbBlue := GetBValue(Color)/255; 
    end; 
end; 

如何獲取從TRGB一個TColor?只要做到

function GetPascalColor(const Color: TRGB): TColor; 
begin 
    with Color do 
    result := Windows.RGB(round(255*rgbRed), 
     round(255*rgbGreen), 
     round(255*rgbBlue)); 
end; 
相關問題