RGB,HSL色彩空間轉換 |
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
1. 為解決問題 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=43725 2. RGB 及 HSL 這兩種色彩空間的轉換 在 Windows 中的 standard COLOR DialogBox 中的兩組常用的色彩空間, 如圖
其中的色調, 濃度, 亮度即為色彩空間中的 HSL (Hue angle, Satuation, Lightness), 整理一下找到的資料, 做出 HSL, RGB 的轉換方式, 以及做出類似自訂色彩中的亮度 bar 的功能, 請參考. 其中 RGB to HSL, 及 HSL to RGB 的 function 列出如下:
// input H, S, L to Get R, G, B // H is 0~360, S, L is 0~1; R, G, B is 0~1 procedure HSL2RGB(H, S, L:Double; var R, G, B: Double); var mx, mn: Double; function f(h, mx, mn: Double): Double; begin f:=0; if h<0 then h:=h+360; if h>=360 then h:=h-360; if (h>=0) and (h<60) then f := mn+(mx-mn)*h/60 else if (h>=60) and (h<180) then f := mx else if (h>=180) and (h<240) then f := mn+(mx-mn)*(240-h)/60 else if (h>=240) then f := mn // h not between 0~360 !! else ShowMessage('h= '+FloatToStr(h)+' error!'); end; begin if L <=0.5 then begin mx := L * (1+S); mn := 2*L-mx; end else begin mx := L*(1-S)+S; mn := 2*L-mx; end; R := f(H+120, mx, mn); G := f(H, mx, mn); B := f(H-120, mx, mn); // Retrieve R, G, B from H, S, L end; // input R, G, B to Get H, S, L // R, G, B is 0~1; H is 0~360, S, L is 0~1 procedure RGB2HSL(R, G, B:Double; var H, S, L:Double); var mx, mn, cd: Double; begin mx := MaxValue([R, G, B]); mn := MinValue([R, G, B]); cd := mx - mn; L := ( mx + mn ) / 2; if(cd=0) then begin H := 0; S := 0; end else begin if(L<=0.5) then S:=cd/(mx+mn) else S:=cd/(2-(mx+mn)); if(R=mx) then H:=(G-B)/cd else if(G=mx) then H:=2+(B-R)/cd else if(B=mx) then H:=4+(R-G)/cd; end; H:=H*60; if(H<0) then H:=H+360; if(H>=360) then H:=H-360; // H, S, L has been converted from R, G, B // The Light Bar is based on value L // H is 0~360, S, L is 0~1!! end;要特別注意的地方在於值域的部分, 在 standard color dialogbox 中的 R, G, B 是 0~255, HSL 是 0~240 哦, 而上面的程式碼用較為一般的值域作法, R, G, B 為 0~1, HSL 中的 H 為色相角 (0~360), S, L 則為 0~1!! 其執行例如下:
附加檔案:43797_lightbar.zip
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |