且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过HSL颜色着色?

更新时间:2023-01-28 10:59:27

我假设,



加法映射




H'= H +(H - H sub base )


(换行)你用于色调确实可能很好。对于饱和度,需要保留灰度值表示乘法映射


= S *(S / S base ),

然而,对于亮度,线性映射不会这样做,因为您要在调整中间值时同时修正0%和100%亮度。自然选择可能是 gamma 类型的地图,即


L'= pow(L,log(L )/ log(L sub base ))= exp log(L)* log(L sub target)/ log(L sub base))。


其中亮度值被缩放到0和1之间。(注意:为了有效地为许多像素计算此地图,您可能需要预计算一个查找表,例如256个条目。)



当然,还有很多其他地图可以使用,但我会从这些开始,看看他们是否给出足够好的结果。请注意,最终,您的结果的质量也可能受到HSL颜色空间的感知非均匀性的限制;有关详情,请参见此***页面


I have an complex problem, but it could be solved via this little problem. I would like to make a colorizer, which works per pixel.

I have a defined base color for the picture (the picture has some pixels with this color and a lot of other pixels, which are near to this color):

Hex: #188DD9
HSL: 204° 80% 47%
RGB: 24 141 217

I know my target base color:

Hex: #23752E
HSL: 128° 54% 30%
RGB: 35 117 46

So, I would like to colorize an image.

My assumption is that if I find the correlation in this two HSL values, I can colorize my picture pixel by pixel.

Currently I found that if I move the base color hue with (target hue - base hue) = -76, the hue will be fine.

Could you direct me where is the connexion between saturation and lightness to solve this problem?

I assume that, whatever mapping you end up using, you want to map black to black, white to white and grays to grays.

The additive mapping

H' = H + (Htarget - Hbase)

(with wrap-around) you use for hue is indeed probably fine. For saturation, the need to preserve gray values suggest a multiplicative mapping

S' = S * (Starget / Sbase),

with values exceeding 100% saturation clipped. However, for lightness, a linear map just isn't going to do it, since you want to fix both 0% and 100% lightness while adjusting intermediate values. A natural choice might instead be a gamma-type map, i.e.

L' = pow( L, log(Ltarget) / log(Lbase) ) = exp( log(L) * log(Ltarget) / log(Lbase) ).

where the lightness values are scaled to between 0 and 1. (Note: To calculate this map efficiently for lots of pixels, you probably want to precompute a lookup table of, say, 256 entries.)

Of course, there are plenty of other maps you could use, but I'd start with these and see if they give good enough results. Note that, ultimately, the quality of your results may also be limited by the perceptual non-uniformity of the HSL color space; for details, see this Wikipedia page.