def is_it_red(hex_color):
    # Função simples que verificar se a cor dada tem mais de 50% de vermelho no sistema RGB
    # Retorna preto ou vermelho, porque será usada para representar graficamente o resultado
    #A função hex_to_rgb_percent retorna um string na forma 'x%' e logo tem que ser formatado para ser convertido em float
    red_percent = float((wc.hex_to_rgb_percent(hex_color)[0]).split('%')[0]) 
    if red_percent >= 50: 
        return 'red'
    else:
        return 'black'
Exemple #2
0
    def test_hex_to_rgb_percent(self):
        """
        Test conversion from hex to percent RGB triplet.
        """
        test_pairs = ((u'#fff', (u'100%', u'100%', u'100%')),
                      (u'#ffffff', (u'100%', u'100%', u'100%')),
                      (u'#000080', (u'0%', u'0%', u'50%')))

        for pair in test_pairs:
            self.assertEqual(pair[1], webcolors.hex_to_rgb_percent(pair[0]))
    def test_hex_to_rgb_percent(self):
        """
        Test conversion from hex to percent RGB triplet.
        """
        test_pairs = ((u'#fff', (u'100%', u'100%', u'100%')),
                      (u'#ffffff', (u'100%', u'100%', u'100%')),
                      (u'#000080', (u'0%', u'0%', u'50%')))

        for pair in test_pairs:
            self.assertEqual(pair[1],
                             webcolors.hex_to_rgb_percent(pair[0]))
Exemple #4
0
def get_color_info(rgb_id):
    """Return everything we know about this rgb_id (an RGB integer)."""
    hue_name = "Unknown"
    hex_code = _get_hex(rgb_id)
    hex_hash = title = f"#{hex_code}"
    red, green, blue = _get_rgb_from_token(rgb_id)
    title = hex_hash.upper()

    # Gather color stats and attributes.
    rgb_percent = webcolors.hex_to_rgb_percent(hex_hash)
    lum = _get_luminance(red, green, blue)
    h, s, v = colorsys.rgb_to_hsv(red / 255.0, green / 255.0, blue / 255.0)
    hue = int(math.floor(h * 360.0))
    saturation = int(math.floor(s * 255.0))
    value = int(math.floor(v * 255.0))

    if hue == 0 and saturation == 0 and value == 0:
        hue_name = "Black"
    elif hue == 0 and saturation == 0 and value == 255:
        hue_name = "White"
    else:
        for colors in HUES:
            if hue in range(colors[1], colors[2]):
                hue_name = colors[0]

    # Normalize user-provided custom names for colors.
    title_is_hex = title.startswith('#')  # Implementation will change later
    if not title_is_hex:
        # We have a named color. Remove all punctuation from the string.
        title = title.translate(str.maketrans('', '',
                                              string.punctuation)).title()

    # Generate image assets and upload them to Google Storage Cloud.
    url = _compose_image(rgb_id, red, green, blue, lum)

    return {
        'title': f"{title} ({hue_name})",
        'description': f'A {hue_name.lower()} colored RainbowCoin.',
        'rgb_integer': int(rgb_id),
        'hex_code': hex_hash,
        'luminance': float("%.2f" % ((float(lum) / 255.0) * 100)),
        'rgb': f'({red}, {green}, {blue})',
        'hsv': f'({hue}, {saturation}, {value})',
        'hue_name': hue_name,
        'image': url,
        'red': red,
        'green': green,
        'blue': blue,
    }
    def test_hex_to_rgb_percent(self):
        """
        Test conversion from hex to percent RGB triplet.

        """
        test_pairs = (
            ("#fff", ("100%", "100%", "100%")),
            ("#ffffff", ("100%", "100%", "100%")),
            ("#000080", ("0%", "0%", "50%")),
        )

        for hex_value, triplet in test_pairs:
            result = webcolors.hex_to_rgb_percent(hex_value)
            assert isinstance(result, webcolors.PercentRGB)
            assert triplet == result
Exemple #6
0
    def test_hex_to_rgb_percent(self):
        """
        Test conversion from hex to percent RGB triplet.

        """
        test_pairs = (
            (u'#fff', (u'100%', u'100%', u'100%')),
            (u'#ffffff', (u'100%', u'100%', u'100%')),
            (u'#000080', (u'0%', u'0%', u'50%'))
        )

        for hex_value, triplet in test_pairs:
            result = webcolors.hex_to_rgb_percent(hex_value)
            assert isinstance(result, webcolors.PercentRGB)
            assert triplet == result
    def test_hex_to_rgb_percent(self):
        """
        Test conversion from hex to percent RGB triplet.

        """
        test_pairs = (
            (u'#fff', (u'100%', u'100%', u'100%')),
            (u'#ffffff', (u'100%', u'100%', u'100%')),
            (u'#000080', (u'0%', u'0%', u'50%'))
        )

        for hex_value, triplet in test_pairs:
            result = webcolors.hex_to_rgb_percent(hex_value)
            assert isinstance(result, webcolors.PercentRGB)
            assert triplet == result
Exemple #8
0
def hex_to_rgb_percent(hex_str):
    color = webcolors.hex_to_rgb_percent(hex_str)
    return [float(i.strip('%'))/100.0 for i in color]
Exemple #9
0
 def hex_print(self, hex_str, str, end=' '):
     # print(str)
     color = webcolors.hex_to_rgb_percent(hex_str)
     console.set_color(*[float(i.strip('%'))/100.0 for i in color])
     print(str, end=end)
     console.set_color()