def adjust_color( color, red=None, green=None, blue=None, hue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = hue or saturation or lightness if do_rgb and do_hsl: raise ValueError( "Can't adjust both RGB and HSL channels at the same time") zero = Number(0) a = color.alpha + (alpha or zero).value if do_rgb: r, g, b = color.rgba[:3] channels = [ current + (adjustment or zero).value / 255 for (current, adjustment) in zip(color.rgba, (red, green, blue))] return Color.from_rgb(*channels, alpha=a) else: h, s, l = color.hsl h = (h + (hue or zero).value / 360) % 1 s += _interpret_percentage(saturation or zero, relto=100, clamp=False) l += _interpret_percentage(lightness or zero, relto=100, clamp=False) return Color.from_hsl(h, s, l, a)
def adjust_color( color, red=None, green=None, blue=None, hue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = hue or saturation or lightness if do_rgb and do_hsl: raise ValueError( "Can't adjust both RGB and HSL channels at the same time") zero = Number(0) a = color.alpha + (alpha or zero).value if do_rgb: r, g, b = color.rgba[:3] channels = [ current + (adjustment or zero).value / 255 for (current, adjustment) in zip(color.rgba, (red, green, blue))] return Color.from_rgb(*channels, alpha=a) else: h, s, l = color.hsl h = (h + (hue or zero).value / 360) % 1 s += _interpret_percentage(saturation or zero, relto=100, clamp=False) l += _interpret_percentage(lightness or zero, relto=100, clamp=False) return Color.from_hsl(h, s, l, a)
def change_color(color, red=None, green=None, blue=None, hue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = hue or saturation or lightness if do_rgb and do_hsl: raise ValueError("Can't change both RGB and HSL channels at the same time") if alpha is None: alpha = color.alpha else: alpha = alpha.value if do_rgb: channels = list(color.rgba[:3]) if red: channels[0] = _interpret_percentage(red, relto=255) if green: channels[1] = _interpret_percentage(green, relto=255) if blue: channels[2] = _interpret_percentage(blue, relto=255) return Color.from_rgb(*channels, alpha=alpha) else: channels = list(color.hsl) if hue: expect_type(hue, Number, unit=None) channels[0] = (hue.value / 360) % 1 # Ruby sass treats plain numbers for saturation and lightness as though # they were percentages, just without the % if saturation: channels[1] = _interpret_percentage(saturation, relto=100) if lightness: channels[2] = _interpret_percentage(lightness, relto=100) return Color.from_hsl(*channels, alpha=alpha)
def scale_color(color, red=None, green=None, blue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = saturation or lightness if do_rgb and do_hsl: raise ValueError( "Can't scale both RGB and HSL channels at the same time") scaled_alpha = _scale_channel(color.alpha, alpha) if do_rgb: channels = [ _scale_channel(channel, scaleby) for channel, scaleby in zip(color.rgba, (red, green, blue)) ] return Color.from_rgb(*channels, alpha=scaled_alpha) else: channels = [ _scale_channel(channel, scaleby) for channel, scaleby in zip(color.hsl, (None, saturation, lightness)) ] return Color.from_hsl(*channels, alpha=scaled_alpha)
def change_color(color, red=None, green=None, blue=None, hue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = hue or saturation or lightness if do_rgb and do_hsl: raise ValueError("Can't change both RGB and HSL channels at the same time") if alpha is None: alpha = color.alpha else: alpha = alpha.value if do_rgb: channels = list(color.rgba[:3]) if red: channels[0] = _interpret_percentage(red, relto=255) if green: channels[1] = _interpret_percentage(green, relto=255) if blue: channels[2] = _interpret_percentage(blue, relto=255) return Color.from_rgb(*channels, alpha=alpha) else: channels = list(color.hsl) if hue: expect_type(hue, Number, unit=None) channels[0] = (hue.value / 360) % 1 # Ruby sass treats plain numbers for saturation and lightness as though # they were percentages, just without the % if saturation: channels[1] = _interpret_percentage(saturation, relto=100) if lightness: channels[2] = _interpret_percentage(lightness, relto=100) return Color.from_hsl(*channels, alpha=alpha)
def hsla(h, s, l, a): return Color.from_hsl( h.value / 360 % 1, # Ruby sass treats plain numbers for saturation and lightness as though # they were percentages, just without the % _interpret_percentage(s, relto=100), _interpret_percentage(l, relto=100), alpha=a.value, )
def hsla(h, s, l, a): return Color.from_hsl( h.value / 360 % 1, # Ruby sass treats plain numbers for saturation and lightness as though # they were percentages, just without the % _interpret_percentage(s, relto=100), _interpret_percentage(l, relto=100), alpha=a.value, )
def scale_color(color, red=None, green=None, blue=None, saturation=None, lightness=None, alpha=None): do_rgb = red or green or blue do_hsl = saturation or lightness if do_rgb and do_hsl: raise ValueError("Can't scale both RGB and HSL channels at the same time") scaled_alpha = _scale_channel(color.alpha, alpha) if do_rgb: channels = [_scale_channel(channel, scaleby) for channel, scaleby in zip(color.rgba, (red, green, blue))] return Color.from_rgb(*channels, alpha=scaled_alpha) else: channels = [ _scale_channel(channel, scaleby) for channel, scaleby in zip(color.hsl, (None, saturation, lightness)) ] return Color.from_hsl(*channels, alpha=scaled_alpha)
def complement(color): h, s, l = color.hsl return Color.from_hsl((h + 0.5) % 1, s, l, alpha=color.alpha)
def adjust_hue(color, degrees): h, s, l = color.hsl delta = degrees.value / 360 return Color.from_hsl((h + delta) % 1, s, l, alpha=color.alpha)
def greyscale(color): h, s, l = color.hsl return Color.from_hsl(h, 0, l, alpha=color.alpha)
def complement(color): h, s, l = color.hsl return Color.from_hsl((h + 0.5) % 1, s, l, alpha=color.alpha)
def adjust_hue(color, degrees): h, s, l = color.hsl delta = degrees.value / 360 return Color.from_hsl((h + delta) % 1, s, l, alpha=color.alpha)
def greyscale(color): h, s, l = color.hsl return Color.from_hsl(h, 0, l, alpha=color.alpha)