Beispiel #1
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
Beispiel #2
0
def test_special_types():
    obj, sig = dbus_prepare(Color.NewFromHtml('black'))
    assert obj == '#000000'
    assert sig == 's'

    obj, sig = dbus_prepare(Int(5))
    assert isinstance(obj, dict)
    assert sig == 'a{sv}'
    for value in obj.values():
        assert isinstance(value, GLib.Variant)

    obj, sig = dbus_prepare(EnumTest)
    assert isinstance(obj, tuple)
    assert sig == '(sss)'
Beispiel #3
0
def to_rgb(arg) -> tuple:
    """
    Convert various representations to RGB tuples

    :return: An RGB int tuple
    """
    if arg is None:
        return (0, 0, 0)
    if isinstance(arg, Color):
        return arg.intTuple[:3]
    if isinstance(arg, str):
        return Color.NewFromHtml(arg).intTuple[:3]
    if isinstance(arg, tuple) or isinstance(arg, list):
        if arg[0] is None:
            return (0, 0, 0)

        if isinstance(arg[0], list) or isinstance(arg[0], tuple) \
                or isinstance(arg[0], str) or isinstance(arg[0], Color):
            return [to_rgb(item) for item in arg]
        return rgb_to_int_tuple(arg)

    raise TypeError('Unable to parse color from \'%s\' (%s)' % (arg, type(arg)))
Beispiel #4
0
 def __init__(self, first, second):
     self.first = Color.NewFromHtml(first)
     self.second = Color.NewFromHtml(second)
Beispiel #5
0
 def __init__(self, hwid, caps):
     self._hardware_id = hwid
     self._rgb = caps.get('rgb', False)
     self._color = Color.NewFromHtml(caps.get('color', 'green'))
     self._has_modes = caps.get('has_modes', False)
Beispiel #6
0
 def from_html(cls, html_string):
     rgb = GC.NewFromHtml(html_string).rgb
     return cls("rgb", *rgb)
Beispiel #7
0
def construct_color(loader, node):
    val = loader.construct_yaml_str(node)
    return Color.NewFromHtml(val)
Beispiel #8
0
from PIL import Image, ImageDraw
import sqlite3
from grapefruit import Color

c1 = Color.NewFromHtml('#ffffff')
c2 = Color.NewFromHtml('#660000')

conn = sqlite3.connect('zon-tags-2.db')
im = Image.new('RGBA', (20 * 52 + 60, (2012 - 1946) * 20 + 40))

sql = "SELECT substr(id, 0, 5) year, substr(id, 6, 2) issue, count(*) from articles group by year, issue
 order by year, issue"

draw = ImageDraw.Draw(im)

for y in range(1946, 2013):
    draw.text((5, 32 + (y - 1946) * 20), str(y), fill='black')

for i in range(1, 53):
    draw.text((36 + (i) * 20, 10), str(i), fill='black')

for res in conn.execute(sql):
    year, issue, count = map(int, res)
    y = 30 + (year - 1946) * 20
    x = 30 + issue * 20
    col = c2.Blend(c1, percent=count / 561.0)
    draw.rectangle((x, y, x + 19, y + 19), fill=col.html)

im.save('heatmap-part.png', 'PNG')
Beispiel #9
0
from grapefruit import Color

for k in Color.NAMED_COLOR:
    c = Color.NewFromHtml(Color.NAMED_COLOR[k],
                          wref=Color.WHITE_REFERENCE['std_D50'])
    out = [k]
    out.append(c.html)
    r, g, b = c.rgb
    out.append([int(r * 255), int(g * 255), int(b * 255)])
    out.append(list(c.hsv))
    out.append(list(c.hsl))
    l, a, b = c.lab
    out.append([l / 100.0, a, b])
    #print 'colors.push( ' + str(out) +' );'
    print 'colors.' + k + ' = "' + c.html + '"'