怎样实现从RGB颜色值转换到HSL颜色值?

Putao 2002-02-11 04:43:54
怎样实现从RGB颜色值转换到HSL颜色值?
...全文
225 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Putao 2002-02-12
  • 打赏
  • 举报
回复
Thank you!
我该怎么给分呀?
torble 2002-02-11
  • 打赏
  • 举报
回复
没有翻译,不过应该看得懂的~
torble 2002-02-11
  • 打赏
  • 举报
回复
The conversion algorithms for these color spaces are originally from the book Fundamentals of Interactive Computer Graphics by Foley and van Dam.


RGB - HSL

Convert the RBG values to the range 0-1
Example: from the video colors page, colorbar red has R=83%, B=7%, G=7%, or in this scale, R=.83, B=.07, G=.07

Find min and max values of R, B, G
In the example, maxcolor = .83, mincolor=.07

If the max and min colors are the same (ie the color is some kind of grey), S is defined to be 0, and H is undefined but in programs usually written as 0

Otherwise, L = (maxcolor + mincolor)/2
For the example, L = (.83+.07)/2 = .45

Test L.
If L < 0.5, S=(maxcolor-mincolor)/(maxcolor+mincolor)
If L >=0.5, S=(maxcolor-mincolor)/(2.0-maxcolor-mincolor)
For the example, L=0.45 so S=(.83-.07)/(.83+.07) = .84

If R=maxcolor, H = (G-B)/(maxcolor-mincolor)
If G=maxcolor, H = 2.0 + (B-R)/(maxcolor-mincolor)
If B=maxcolor, H = 4.0 + (R-G)/(maxcolor-mincolor)
For the example, R=maxcolor so H = (.07-.07)/(.83-.07) = 0

To use the scaling shown in the video color page, convert L and S back to percentages, and H into an angle in degrees (ie scale it from 0-360)

HSL - RGB
If S=0, define R, G, and B all to L

Otherwise, test L.
If L < 0.5, temp2=L*(1.0+S)
If L >= 0.5, temp2=L+S - L*S
In the colorbar example for colorbar green, H=120, L=52, S=79, so converting to the range 0-1, L=.52, so
temp2=(.52+.79) - (.52*.79) = .899

temp1 = 2.0*L - temp2
In the example, temp1 = 2.0*.52 - .899 = .141

Convert H to the range 0-1
In the example, H=120/360 = .33

For each of R, G, B, compute another temporary value, temp3, as follows:
for R, temp3=H+1.0/3.0
for G, temp3=H
for B, temp3=H-1.0/3.0
if temp3 < 0, temp3 = temp3 + 1.0
In the example, Rtemp3=.33+.33 = .66, Gtemp3=.33, Btemp3=.33-.33=0

For each of R, G, B, do the following test:
If 6.0*temp3 < 1, color=temp1+(temp2-temp1)*6.0*temp3
Else if 2.0*temp3 < 1, color=temp2
Else if 3.0*temp3 < 2, color=temp1+(temp2-temp1)*((2.0/3.0)-temp3)*6.0
In the example,
3.0*Rtemp3 < 2 so R=.141+(.899-.141)*((2.0/3.0-.66)*6.0=.141
2.0*Gtemp3 < 1 so G=.899
6.0*Btemp3 < 1 so B=.141+(.899-.141)*6.0*0=.141


Scale back to the range 0-100 to use the scaling shown in the video color page
For the example, R=14, G=90, B=14
Putao 2002-02-11
  • 打赏
  • 举报
回复
:-)
brallow 2002-02-11
  • 打赏
  • 举报
回复
我不知道!

829

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧