700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ HLS HSV)之间的转换

python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ HLS HSV)之间的转换

时间:2023-01-12 13:01:41

相关推荐

python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ HLS HSV)之间的转换

"""Conversion functions between RGB and other color systems.RGB和其他色彩系统之间的转换功能。This modules provides two functions for each color system ABC:该模块为每个颜色系统ABC提供两个功能:rgb_to_abc(r, g, b) --> a, b, cabc_to_rgb(a, b, c) --> r, g, bAll inputs and outputs are triples of floats in the range [0.0...1.0](with the exception of I and Q, which covers a slightly larger range).Inputs outside the valid range may cause exceptions or invalid outputs.所有输入和输出都是在[0.0 ... 1.0]范围内的三进制浮点数(I和Q除外,后者覆盖的范围稍大)。 输入超出有效范围可能会导致异常或无效输出。Supported color systems: 支持的颜色系统:RGB: Red, Green, Blue components 红色,绿色,蓝色组件YIQ: Luminance, Chrominance (used by composite video signals) 亮度,色度(由复合视频信号使用)HLS: Hue, Luminance, Saturation 色相,亮度,饱和度HSV: Hue, Saturation, Value 色相,饱和度,亮度"""# References:# /wiki/YIQ# /wiki/HLS_color_space# /wiki/HSV_color_space__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb","rgb_to_hsv","hsv_to_rgb"]# Some floating point constants 一些浮点常量ONE_THIRD = 1.0/3.0ONE_SIXTH = 1.0/6.0TWO_THIRD = 2.0/3.0# YIQ: used by composite video signals (linear combinations of RGB) 用于复合视频信号(RGB的线性组合)# Y: perceived grey level (0.0 == black, 1.0 == white) 感知灰度(0.0 ==黑色,1.0 ==白色)# I, Q: color components 颜色成分## There are a great many versions of the constants used in these formulae. 这些公式中使用了很多常数。# The ones in this library uses constants from the FCC version of NTSC. 该库中的变量使用FSC版本的NTSC中的常量。def rgb_to_yiq(r, g, b):y = 0.30*r + 0.59*g + 0.11*bi = 0.74*(r-y) - 0.27*(b-y)q = 0.48*(r-y) + 0.41*(b-y)return (y, i, q)def yiq_to_rgb(y, i, q):# r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)# b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)# g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59r = y + 0.9468822170900693*i + 0.6235565819861433*qg = y - 0.27478764629897834*i - 0.6356910791873801*qb = y - 1.1085450346420322*i + 1.7090069284064666*qif r < 0.0:r = 0.0if g < 0.0:g = 0.0if b < 0.0:b = 0.0if r > 1.0:r = 1.0if g > 1.0:g = 1.0if b > 1.0:b = 1.0return (r, g, b)# HLS: Hue, Luminance, Saturation 色相,亮度,饱和度# H: position in the spectrum 在频谱中的位置# L: color lightness 颜色明度# S: color saturation 颜色饱和度def rgb_to_hls(r, g, b):maxc = max(r, g, b)minc = min(r, g, b)# XXX Can optimize (maxc+minc) and (maxc-minc) 可以优化(maxc + minc)和(maxc-minc)l = (minc+maxc)/2.0if minc == maxc:return 0.0, l, 0.0if l <= 0.5:s = (maxc-minc) / (maxc+minc)else:s = (maxc-minc) / (2.0-maxc-minc)rc = (maxc-r) / (maxc-minc)gc = (maxc-g) / (maxc-minc)bc = (maxc-b) / (maxc-minc)if r == maxc:h = bc-gcelif g == maxc:h = 2.0+rc-bcelse:h = 4.0+gc-rch = (h/6.0) % 1.0return h, l, sdef hls_to_rgb(h, l, s):if s == 0.0:return l, l, lif l <= 0.5:m2 = l * (1.0+s)else:m2 = l+s-(l*s)m1 = 2.0*l - m2return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))def _v(m1, m2, hue):hue = hue % 1.0if hue < ONE_SIXTH:return m1 + (m2-m1)*hue*6.0if hue < 0.5:return m2if hue < TWO_THIRD:return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0return m1# HSV: Hue, Saturation, Value 色相,饱和度,亮度# H: position in the spectrum 在频谱中的位置# S: color saturation ("purity") 颜色饱和度(“纯度”)# V: color brightness 色彩亮度def rgb_to_hsv(r, g, b):maxc = max(r, g, b)minc = min(r, g, b)v = maxcif minc == maxc:return 0.0, 0.0, vs = (maxc-minc) / maxcrc = (maxc-r) / (maxc-minc)gc = (maxc-g) / (maxc-minc)bc = (maxc-b) / (maxc-minc)if r == maxc:h = bc-gcelif g == maxc:h = 2.0+rc-bcelse:h = 4.0+gc-rch = (h/6.0) % 1.0return h, s, vdef hsv_to_rgb(h, s, v):if s == 0.0:return v, v, vi = int(h*6.0) # XXX assume int() truncates! 假设int()会截断!f = (h*6.0) - ip = v*(1.0 - s)q = v*(1.0 - s*f)t = v*(1.0 - s*(1.0-f))i = i%6if i == 0:return v, t, pif i == 1:return q, v, pif i == 2:return p, v, tif i == 3:return p, q, vif i == 4:return t, p, vif i == 5:return v, p, q# Cannot get here 无法到达这里

注意,这边将hsv转换成rgb后还得分配给0-255:

colors = [(1.0, 0.0, 0.0)]colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))print(colors)# [(255, 0, 0)]红色

python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ HLS HSV)之间的转换 hsv_to_rgb(h s v)函数

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。