日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/16 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ、HLS、HSV)之间的转换 hsv_to_rgb(h, s, v)函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
"""Conversion functions between RGB and other color systems. RGB和其他色彩系統(tǒng)之間的轉(zhuǎn)換功能。This modules provides two functions for each color system ABC: 該模塊為每個顏色系統(tǒng)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]范圍內(nèi)的三進(jìn)制浮點(diǎn)數(shù)(I和Q除外,后者覆蓋的范圍稍大)。 輸入超出有效范圍可能會導(dǎo)致異常或無效輸出。Supported color systems: 支持的顏色系統(tǒng): RGB: Red, Green, Blue components 紅色,綠色,藍(lán)色組件 YIQ: Luminance, Chrominance (used by composite video signals) 亮度,色度(由復(fù)合視頻信號使用) 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 一些浮點(diǎn)常量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) 用于復(fù)合視頻信號(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. 這些公式中使用了很多常數(shù)。 # 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) 可以優(yōu)化(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! 假設(shè)int()會截?cái)?#xff01;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 無法到達(dá)這里

注意,這邊將hsv轉(zhuǎn)換成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)]紅色

總結(jié)

以上是生活随笔為你收集整理的python colorsys模块 RGB和其他色彩系统(颜色空间)(YIQ、HLS、HSV)之间的转换 hsv_to_rgb(h, s, v)函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。