def get_color():
    infinite_color = itertools.cycle(go_and_back_colors)
    while True:
        current_color = next(infinite_color)
        new_color = Color()
        new_color.set_rgb(map(lambda x: x / 256, current_color))
        yield new_color.get_hex()
Exemple #2
0
def _identify_and_create_object(user_input: str) -> (str, Color):
    """
    Identifies the data type of an input, converts it appropriately and creates the Color() object
    :param user_input: The input string from the input textbox
    :return: A tuple containing a string (the type detected) and a
             Color object that corresponds to the user input
    :raises InvalidColorError: if the inputted color doesn't fit any of the available formats
    """
    result = Color()

    # Checks for literal color names
    tmp = re.sub(r"[^a-zA-Z]+", "", user_input.lower())
    if tmp in COLOR_NAMES:
        result = Color(tmp)
        print(f'\nDetected literal: {tmp}')
        return 'literal', result

    # Matches a hex color without the '#' sign
    hex_without_pound_sign_pattern = re.compile(r'[A-Fa-f0-9]{6}')
    if hex_without_pound_sign_pattern.match(user_input) and len(user_input) == 6:
        result.set_hex_l('#' + user_input.lower())
        print(f"\nDetected hex without '#': {user_input.upper()}")
        return 'hex', result

    # Matches a hex color with the '#' sign
    hex_with_pound_sign_pattern = re.compile(r'#[A-Fa-f0-9]{6}')
    if hex_with_pound_sign_pattern.match(user_input) and len(user_input) == 7:
        result.set_hex_l(user_input.lower())
        print(f"\nDetected hex with '#': {user_input.upper()}")
        return 'hex', result

    # Matches an RGB color (3x 1-3 digits numbers, separated by up to 2 of ',' and ' ')
    rgb_pattern = re.compile(r'(?:[0-9]{1,3}[, ]{1,2}){2}[0-9]{1,3}')
    if rgb_pattern.match(user_input):
        user_input = _replace_separators(user_input)
        r_value, g_value, b_value = [int(val) for val in user_input.split('*')]

        if not 0 <= r_value <= 255 or not 0 <= g_value <= 255 or not 0 <= b_value <= 255:
            raise InvalidColorError(
                    'This looks like an RGB color but the values are not in the 0-255 range!')
        result.set_rgb((r_value / 255, g_value / 255, b_value / 255))
        print(f"\nDetected rgb: {user_input}")
        return 'rgb', result

    # Matches a HSL color (1x 1-3 digits number, then 2x percentages or numbers in the range 0-1)
    hsl_pattern = re.compile(r'[0-9]{1,3}(?:[, ]{1,2}(?:[01]\.[0-9]{1,2}|[0-9]{1,3}%)){2}')
    if hsl_pattern.match(user_input):
        user_input = _replace_separators(user_input)
        h_value, s_value, l_value = user_input.split('*')
        h_value = int(h_value)
        if not 0 <= h_value <= 360:
            raise InvalidColorError(
                    'This looks like a HSL color but the Hue value is not in the 0-360 range!')

        try:
            s_value = _transform_percentage(s_value)
            l_value = _transform_percentage(l_value)
        except ValueError:
            raise InvalidColorError('This looks like a HSL color but the Saturation or Lightness '
                                    'are not valid percentages or in the 0.0-1.0 range!')

        if not 0 <= s_value <= 1 or not 0 <= l_value <= 1:
            raise InvalidColorError(
                    'This looks like a HSL color but the Saturation or Lightness values are not '
                    'percentages or in the 0-1 range!')
        result.set_hsl((h_value / 360, s_value, l_value))
        print(f"\nDetected hsl: {user_input}")
        return 'hsl', result

    raise InvalidColorError("The input doesn't fit any of the known color patterns!")
Exemple #3
0
 def get_color(self):
     color = Color()
     color.set_rgb(self.rgbs[0, :]) 
     return color
Exemple #4
0
def random_color():
    color = Color()
    color.set_rgb([1 - 0.5 * random() for x in range(3)])
    return color
Exemple #5
0
 def get_color(self):
     color = Color()
     color.set_rgb(self.rgbs[0, :])
     return color
Exemple #6
0
 def complementary(self, color):
     r, g, b = color.get_rgb()
     k = self.hilo(r, g, b)
     complement = Color()
     complement.set_rgb((k - r, k - g, k - b))
     return complement