Exemplo n.º 1
0
    def health_color(self):
        a = self.dead_color.hsl
        b = self.healthy_color.hsl

        factor = self.health_percentage

        # This is not great Need real game vector math library
        # Also a color library with no mix or lerp ? (Or i just can't read and find it)
        # lerp between colors in hsl and then convert to rgb.
        result = (Health.lerp(a[x], b[x], factor) for x in range(3))

        ret_val = Color()
        ret_val.set_hsl(result)
        ret_val = ret_val.rgb

        # Apply HRD type stuff
        light_power = Health.lerp(self.light_power_dead, self.light_Power_full,
                                  self.health_percentage)
        # Oh well.
        ret_val = (
            ret_val[0] * light_power,
            ret_val[1] * light_power,
            ret_val[2] * light_power,
        )

        return ret_val
Exemplo n.º 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!")
Exemplo n.º 3
0
def lighter_color(color: ColorStr) -> ColorStr:
    c = Color(color)
    h, s, l = c.get_hsl()
    l_new = l + (1.0 - l) * 0.5
    c.set_hsl((h, s, l_new))
    return c.get_hex()
Exemplo n.º 4
0
def darker_color(color: ColorStr) -> ColorStr:
    c = Color(color)
    h, s, l = c.get_hsl()
    l_new = l * 0.75  # l - (1.0-l)*0.5
    c.set_hsl((h, s, l_new))
    return c.get_hex()