def default_color_function(ctx, z):
if ctx.isinf(z):
return (1.0, 1.0, 1.0)
if ctx.isnan(z):
return (0.5, 0.5, 0.5)
pi = 3.1415926535898
a = (float(ctx.arg(z)) + ctx.pi) / (2*ctx.pi)
a = (a + 0.5) % 1.0
b = 1.0 - float(1/(1.0+abs(z)**0.3))
return hls_to_rgb(a, b, 0.8)
python类hls_to_rgb()的实例源码
def __init__(self, templateData, parcelationData, axis, correlationTable, colorTable, selectedColor):
super(SliceViewer, self).__init__()
self.template = templateData
self.regionId = None
self.parcelation = parcelationData
self.axis = axis
self.CommunityMode = False
self.correlationTable = correlationTable
self.colorTable= colorTable
self.selectedColor = selectedColor
self.displayedSlice = 0
self.QImage = []
scalefactor = 350
self.scaleFactor = int(math.ceil(scalefactor / self.parcelation.shape[0]))
numColors = self.parcelation.max()
self.clut = np.zeros(numColors, dtype=np.uint32)
for i in range(numColors):
r, g, b = colorsys.hls_to_rgb(float(i) / float(numColors), 0.5, 1.0)
self.clut[i] = (255 << 24 | int(255*r) << 16 | int(255*g) << 8 | int(255*b))
slice_view_layout = QtGui.QHBoxLayout()
self.setLayout(slice_view_layout)
slider = QtGui.QSlider()
slider.setRange(0, self.template.shape[self.axis]-1)
slider.valueChanged.connect(self.setDisplayedSlice)
slider.sliderReleased.connect(self.handleSliderRelease)
slider.setValue(self.displayedSlice)
slice_view_layout.addWidget(slider)
self.label = QtGui.QLabel()
self.updateSliceLabel()
slice_view_layout.addWidget(self.label)
def ColorForCommunities(self,counter):
k=self.updateCommunityColors(counter)
self.ColorVisit = []
m = len(self.oneLengthCommunities)
for i in range(counter):
if i in self.oneLengthCommunities:
r,g,b = 0.5,0.5,0.5
self.clut[i] = (255 << 24 | int(255*r) << 16 | int(255*g) << 8 | int(255*b))
self.ColorVisit.append((r*255,g*255,b*255,255))
else:
r, g, b = colorsys.hls_to_rgb(float(i) / float(counter-m), 0.8, 0.9)
self.clut[i] = (255 << 24 | int(255*r) << 16 | int(255*g) << 8 | int(255*b))
self.ColorVisit.append((r*255,g*255,b*255,255))
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def rgb_lighten_saturate(rgb, dl,ds):
hls = colorsys.rgb_to_hls(*rgb)
hls = (hls[0], hls[1] + dl, hls[2] + ds)
return colorsys.hls_to_rgb(*hls)
def hex_lighten_saturate(hexstr, dl, ds):
hls = colorsys.rgb_to_hls(*hex2rgb(hexstr))
hls = (hls[0], hls[1] + dl, hls[2] + ds)
return rgb2hex(colorsys.hls_to_rgb(*hls))
def rgb_lighten_saturate(rgb, amount):
hls = colorsys.rgb_to_hls(*rgb)
hls = (hls[0], hls[1] + dl, hls[2] + ds)
return colorsys.hls_to_rgb(*hls)
def hex_lighten_saturate(hexstr, dl,ds):
hls = colorsys.rgb_to_hls(*hex2rgb(hexstr))
hls = (hls[0], hls[1] + dl, hls[2] + ds)
return rgb2hex(colorsys.hls_to_rgb(*hls))
def from_hls(cls, h, l, s):
"""
Construct a :class:`Color` from `HLS`_ (hue, lightness, saturation)
floats between 0.0 and 1.0.
"""
return super(Color, cls).__new__(cls, *colorsys.hls_to_rgb(h, l, s))
def create_poster(net, icon_size, poster_path):
icon_size_2 = (icon_size + 2)
poster = Image.new('RGB', (icon_size_2 * 20, icon_size_2 * 15), color=(255, 255, 255))
net.run_step(embed([icon_size + 1], icon_size + 2), True)
for x in range(20):
for y in range(15):
icon_coos = generate_icon(net, icon_size)
rgb = colorsys.hls_to_rgb(random.random(), 0.30, 0.9)
rgb = tuple(int(v * 255) for v in rgb)
for x1, y1 in icon_coos:
poster.putpixel((icon_size_2 * x + x1, icon_size_2 * y + y1), rgb)
poster.save(poster_path)
def fromhls(self, h, l, s):
"""Convert to RGB from HSL."""
r, g, b = hls_to_rgb(h, l, s)
self.r = round_int(r * 255.0) & 0xFF
self.g = round_int(g * 255.0) & 0xFF
self.b = round_int(b * 255.0) & 0xFF
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def default_color_function(ctx, z):
if ctx.isinf(z):
return (1.0, 1.0, 1.0)
if ctx.isnan(z):
return (0.5, 0.5, 0.5)
pi = 3.1415926535898
a = (float(ctx.arg(z)) + ctx.pi) / (2*ctx.pi)
a = (a + 0.5) % 1.0
b = 1.0 - float(1/(1.0+abs(z)**0.3))
return hls_to_rgb(a, b, 0.8)
def nick_color(nick):
"""
Generate a color from a given string.
:param str nick: Input string
:return: A hex color
:rtype: str
>>> nick_color("A")
'005ad0'
>>> nick_color("Hello!")
'f20c1a'
"""
try:
return STATIC_COLOR[nick.lower()]
except KeyError:
pass
nick_int = sum(ord(n)*3 for n in nick)
hue = (nick_int % 100) / 100
color = hls_to_rgb(hue, 0.5, 0.9)
return ''.join(["{:02x}".format(int(c*255)) for c in color])
def test_hls_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.hls_to_rgb(*colorsys.rgb_to_hls(*rgb))
)
def HSLToRGB(hue, saturation, luminosity):
red, green, blue = colorsys.hls_to_rgb(hue, luminosity, saturation)
return int(red * 0xff), int(green * 0xff), int(blue * 0xff)