python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ、HLS、HSV)之间的转换 hsv_to_rgb(h, s, v)函数
生活随笔
收集整理的這篇文章主要介紹了
python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ、HLS、HSV)之间的转换 hsv_to_rgb(h, s, v)函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
"""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:
# http://en.wikipedia.org/wiki/YIQ
# http://en.wikipedia.org/wiki/HLS_color_space
# http://en.wikipedia.org/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.0
ONE_SIXTH = 1.0/6.0
TWO_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)函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 零基础学IT选择软件测试有前途吗?
- 下一篇: 泛微oa 明细数据合计