def tohsv(self):
"""Convert to HSV color format."""
return rgb_to_hsv(self.r * RGB_CHANNEL_SCALE, self.g * RGB_CHANNEL_SCALE, self.b * RGB_CHANNEL_SCALE)
python类rgb_to_hsv()的实例源码
def recolor(tree, add):
""" Recursive part of recolor_strokes and recolor_background """
for child in tree:
if 'style' in child.attrib:
styles = { a : b
for (a, b) in (
x.split(":", 1)
for x in child.attrib['style'].split(';')
if ":" in x
)}
if "fill" in styles or "stroke" in styles:
for key in ("fill", "stroke"):
if key in styles:
# Convert color to HSV
r,g,b,a = html_to_rgb(styles[key])
h,s,v = colorsys.rgb_to_hsv(r,g,b)
# Shift hue
h += add
while h > 1.0 : h -= 1.0
# Convert it back
r,g,b = colorsys.hsv_to_rgb(h,s,v)
# Store
styles[key] = rgb_to_html(r,g,b)
child.attrib["style"] = ";".join(( ":".join((x,styles[x])) for x in styles ))
recolor(child, add)
# Generate different colors for controller icons
fireworks.py 文件源码
项目:house-of-enlightenment
作者: house-of-enlightenment
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def fzero_fountain(start_col=16,
color=(0, 255, 255),
border_color=(255, 0, 0),
border_thickness=10,
height=50):
"""
F-Zero Launcher - make a f-zero speed boost arrow around the start_col
"""
# get 5 pixels to either side to select the 11 columns in this section
cols = map(lambda c: c % STATE.layout.columns, range(start_col - 5, start_col + 5 + 1))
# group them by levels to make an f-zero speed boost arrow
levels = [[cols[5]],
[cols[4], cols[6]],
[cols[3], cols[7]],
[cols[2], cols[8]],
[cols[1], cols[9]],
[cols[0], cols[10]]]
def make_line((i, col)):
# fade the colors on the edges
def get_color():
hsv = colorsys.rgb_to_hsv(color[0] // 255, color[1] // 255, color[2] // 255)
rgb = colorsys.hsv_to_rgb(hsv[0], hsv[1], hsv[2] - (i * 0.12))
return (rgb[0] * 255, rgb[1] * 255, rgb[2] * 255)
return RisingLine(
height=height,
start_col=col,
delay=i * 80,
color=get_color(),
border_color=border_color,
border_thickness=border_thickness)
def _rgb2hs(self, rgb):
r = rgb[0] / 255.0
g = rgb[1] / 255.0
b = rgb[2] / 255.0
h, s, v = colorsys.rgb_to_hsv(r, g, b)
h = int(self._mapRange(h, 0.0, 1.0, 0, 65535))
s = int(self._mapRange(s, 0.0, 1.0, 0, 254))
return (h, s)
def tohsv(self):
"""Convert to HSV color format."""
return rgb_to_hsv(self.r * RGB_CHANNEL_SCALE, self.g * RGB_CHANNEL_SCALE, self.b * RGB_CHANNEL_SCALE)
def _get_mark(self):
"""color of the glyph box in the font view. This accepts a 6 hex digit number.
XXX the FL implementation accepts a
"""
import colorsys
r = (self._object.color&0xff0000)>>16
g = (self._object.color&0xff00)>>8
g = (self._object.color&0xff)>>4
return colorsys.rgb_to_hsv( r, g, b)[0]
def _get_hsv(hexrgb):
hexrgb = hexrgb.lstrip("#") # in case you have Web color specs
r, g, b = (int(hexrgb[i:i+2], 16) / 255.0 for i in range(0,5,2))
return colorsys.rgb_to_hsv(r, g, b)
def rgb_to_hsv(red, green, blue):
"""Convert an RGB tuple to an HSV tuple."""
hue, saturation, value = colorsys.rgb_to_hsv(red/255, green/255, blue/255)
return int(hue*360), int(saturation*100), int(value*100)
def test_hsv_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2):
for b in frange(0.0, 1.0, 0.2):
rgb = (r, g, b)
self.assertTripleEqual(
rgb,
colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb))
)
def rgb_to_hsv(r, g, b, a=255):
hsv = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
hsv255 = [int(each * 255) for each in hsv]
return hsv255
def brighter(self):
if self.highlight:
color = [each / 255.0 for each in self.initcolor]
hsv = colorsys.rgb_to_hsv(color[0], color[1], color[2])
rgb = colorsys.hsv_to_rgb(hsv[0], 0.2, 1)
return [int(each * 255) for each in rgb]
else:
return self.initcolor
def rgb(self, color):
"""Set the color of the device, as represented by either a hex string or a list of 0-255 RGB values"""
try:
red, green, blue = color
except ValueError:
try:
hexcolor = color
reg_match = re.match("^([A-Fa-f0-9]{6})$", hexcolor)
if reg_match:
red = int(hexcolor[:2], 16)
green = int(hexcolor[2:-2], 16)
blue = int(hexcolor[-2:], 16)
else:
print("Error: Color must be in valid hex format.")
return
except ValueError:
print("Error: Color must have one hex value or three 0-255 values.")
return
if not 0 <= red <= 255:
print("Error: Red value out of range! (0-255)")
return
if not 0 <= green <= 255:
print("Error: Green value out of range! (0-255)")
return
if not 0 <= blue <= 255:
print("Error: Blue value out of range! (0-255)")
return
hsv = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255)
hue = int(hsv[0] * 360)
saturation = int(hsv[1] * 100)
brightness = int(hsv[2] * 100)
data = {"hue": {"value": hue}, "sat": {"value": saturation}, "brightness": {"value": brightness}}
self.__put("state", data)
###########################################
# Layout methods
###########################################
def test_hsv_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2):
for b in frange(0.0, 1.0, 0.2):
rgb = (r, g, b)
self.assertTripleEqual(
rgb,
colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb))
)
def test_hsv_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2):
for b in frange(0.0, 1.0, 0.2):
rgb = (r, g, b)
self.assertTripleEqual(
rgb,
colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb))
)
def add_color_to_palette(self):
if (self.curr_palette_string.get() == ""):
self.palette = [] # this is in case the default palette has already been used in a previous build
color = tkColorChooser.askcolor()
dprint("New color added to palette", color)
rgb_color = color[0]
hsv_color = colorsys.rgb_to_hsv(rgb_color[0]/255.0, rgb_color[1]/255.0, rgb_color[2]/255.0)
hsv = {"hue": int(hsv_color[0]*360), "saturation": int(hsv_color[1]*100), "brightness": int(hsv_color[2]*100)}
self.palette.append(hsv)
self.curr_palette_string.set(self.curr_palette_string.get() + json.dumps(hsv) + '\n')
def hwb(self):
"""Hue, Whiteness, Blackness."""
hsv = colorsys.rgb_to_hsv(self.r, self.g, self.b)
h = hsv[0]
w = (1.0 - hsv[1]) * hsv[2]
b = 1.0 - hsv[2]
return [h, w, b]
def rgb2val(rgb, minval, maxval):
"""
This function converts a rgb of value into its original value with reference to the minimum and maximum value.
Parameters
----------
rgb : tuple of floats
The rgb value to be converted.
minval : float
The minimum value of the falsecolour rgb.
maxval : float
The maximum value of the falsecolour rgb.
Returns
-------
original value : float
The orignal float value.
"""
hsv = colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2])
y = hsv[0]*360
orig_val_part1 = ((-1*y) + 250)/250.0
orig_val_part2 = maxval-minval
orig_val = (orig_val_part1*orig_val_part2)+minval
return orig_val
#========================================================================================================
#OCCTOPOLOGY INPUTS
#========================================================================================================
def test_hsv_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2):
for b in frange(0.0, 1.0, 0.2):
rgb = (r, g, b)
self.assertTripleEqual(
rgb,
colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb))
)
def rgb_to_hsv(r, g ,b):
"""Convert R(0-255) G(0-255) B(0-255) to H(0-360) S(0-255) V(0-255).
"""
rgb = [x / 255.0 for x in (r, g, b)]
h, s, v = colorsys.rgb_to_hsv(*rgb)
return (h * 360, s * 255, v * 255)
def drawContours(self, path):
# print("load contours",path)
try:
self.canvas.delete("contour")
except tk.TclError:
pass
import re
self.contours = np.load(path)
ex = self.scene["ex"]
lf = len(self.contours.files)
for a in self.contours.files:
cs = self.contours[a]
h = int(re.findall('\d+', a)[0])
h /= lf
# print("file",a,h)
# print("contours",len(cs))
col = colorsys.rgb_to_hsv(0.7, 0.9, 0.85)
hue = col[0] - h / 2
hue = m.fmod(hue, 1)
col = (hue, max(0, min(col[1], 1)), max(0, min(col[2], 1)))
col = colorsys.hsv_to_rgb(*col)
hexcol = rgb2hex(col)
for c in cs:
if len(c):
cc = [((x[1] - 512) / 1024 * ex * 2, (x[0] - 512) / 1024 * ex * 2) for x in c]
if la.norm(c[-1] - c[0]) < 0.01:
self.canvas.create_polygon(cc, fill="", outline=hexcol, width=7, tag="contour")
else:
self.canvas.create_line(cc, fill=hexcol, width=7, tag="contour")
try:
self.canvas.tag_lower("contour")
except tk.TclError:
pass
sys.stdout.flush()