Exemplo n.º 1
0
def rgb_from_tuple(arg: tuple) -> Color:
    """
    Convert a 3-tuple of ints or floats to a Grapefruit color

    :param arg: The RGB tuple to convert
    :return: The Color object
    """
    if len(arg) >= 3:
        if arg[0] is None:
            return Color.NewFromRgb(0, 0, 0)
        if all(isinstance(n, int) for n in arg):
            return Color.NewFromRgb(*Color.IntTupleToRgb(arg))
        if all(isinstance(n, float) for n in arg):
            return Color.NewFromRgb(*arg)

    raise TypeError('Unable to convert %s (%s) to color' % (arg, type(arg[0])))
Exemplo n.º 2
0
    def _refresh(self):
        try:
            self._refreshing = True

            # state
            value = self._get(LED.Command.GET_LED_STATE)
            if value is not None:
                self.state = bool(value[2])

            # color
            value = self._get(LED.Command.GET_LED_COLOR)
            if value is not None:
                self.color = Color.NewFromRgb(value[2] / 255.0,
                                              value[3] / 255.0,
                                              value[4] / 255.0)

            # mode
            value = self._get(LED.Command.GET_LED_MODE)
            if value is not None:
                self.mode = LEDMode(value[2])

            # brightness
            value = self._get_brightness()
            if value is not None:
                self.brightness = scale_brightness(int(value[2]), True)

        finally:
            self._refreshing = False
Exemplo n.º 3
0
    def interference(length,
                     freq1: float = 0.3,
                     freq2: float = 0.3,
                     freq3: float = 0.3,
                     phase1: float = 0.0,
                     phase2: float = 2.0,
                     phase3: float = 4.0,
                     center: float = 128.0,
                     width: float = 127.0):
        """
        Creates an interference pattern of three sine waves
        """
        phase1 = phase1 * math.pi / 3
        phase2 = phase2 * math.pi / 3
        phase3 = phase3 * math.pi / 3

        center /= 255.0
        width /= 255.0

        gradient = []

        for i in range(0, length):
            r = math.sin(freq1 * i + phase1) * width + center
            g = math.sin(freq2 * i + phase2) * width + center
            b = math.sin(freq3 * i + phase3) * width + center
            gradient.append(Color.NewFromRgb(r, g, b))

        return gradient
Exemplo n.º 4
0
    def gradient(length: int, *colors) -> list:
        """
        Generate a looped gradient from multiple evenly-spaced colors

        Uses the new HSLUV colorspace
        :param length: Total number of entries in the final gradient
        :param colors: Color stops, varargs

        :return: List of colors in the gradient
        """

        luv_colors = [rgb_to_hsluv(to_color(x).rgb) for x in colors]
        luv_colors.append(luv_colors[0])

        steps = max(len(luv_colors), math.floor(length / (len(luv_colors))))
        gradient = []
        for color_idx in range(0, len(luv_colors) - 1):
            start = luv_colors[color_idx]
            end = luv_colors[(color_idx + 1)]

            for interp in range(0, steps):
                amount = float(interp) / float(steps)
                i = ColorUtils._circular_interp(start, end, amount)
                gradient.append(
                    Color.NewFromRgb(*hsluv_to_rgb([i[0], i[1], i[2]])))

        return gradient
Exemplo n.º 5
0
def to_color(*color_args) -> Color:
    """
    Convert various color representations to grapefruit.Color

    Handles RGB triplets, hexcodes, and html color names.

    :return: The color
    """
    colors = []
    for arg in color_args:
        value = None
        if arg is not None:
            if isinstance(arg, Color):
                value = arg
            elif isinstance(arg, str):
                if arg != '':
                    # grapefruit's default str() spews a string repr of a tuple
                    strtuple = COLOR_TUPLE_STR.match(arg)
                    if strtuple:
                        value = Color.NewFromRgb(*[float(x) \
                                for x in strtuple.group(1).split(', ')])
                    else:
                        value = Color.NewFromHtml(arg)
            elif isinstance(arg, Iterable):
                value = rgb_from_tuple(arg)
            else:
                raise TypeError('Unable to parse color from \'%s\' (%s)' % (arg, type(arg)))
        colors.append(value)

    if len(colors) == 0:
        return None
    if len(colors) == 1:
        return colors[0]

    return colors
Exemplo n.º 6
0
    def inverse(color: ColorType) -> float:
        """
        Get the RGB inverse of this color (1 - component)

        :param color: a color
        :return: Inverse of the given color
        """
        rgb = color.rgb
        return Color.NewFromRgb(1.0 - rgb[0], 1.0 - rgb[1], 1.0 - rgb[2], color.alpha)
Exemplo n.º 7
0
    def hsv_gradient(color1: ColorType, color2: ColorType, steps: int) -> list:
        """
        Generate a gradient between two points in HSV colorspace

        :param color1: Starting color
        :param color2: Ending color
        :param steps: Number of steps in the gradient
        :param loop: If the gradient should "loop" back around to it's starting point

        :return: List of colors in the gradient
        """
        start = ColorUtils._hsva(color1)
        end = ColorUtils._hsva(color2)

        gradient = []
        for x in range(0, steps):
            amount = float(x) / float(steps - 1)
            i = ColorUtils._circular_interp(start, end, amount)
            gradient.append(Color.NewFromRgb(*hsluv_to_rgb([i[0], i[1], i[2]])))

        return gradient