示例#1
0
文件: daemon.py 项目: LED-Freaks/LedD
def set_color(**kwargs):
    """
    Part of the Color API. Used to set color of a stripe.
    Required parameters: stripe ID: sid; HSV values hsv: h,s,v, controller id: cid
    """

    if "sid" not in kwargs or "hsv" not in kwargs:
        return JSONRPCInvalidParams()

    stripe = get_stripe(kwargs['sid'])

    if stripe:
        try:
            stripe.set_color(spectra.hsv(kwargs['hsv']['h'], kwargs['hsv']['s'], kwargs['hsv']['v']))
        except OSError as e:
            if int(e) == errno.ECOMM:
                log.warning("Communication error on I2C Bus")
                return e
            else:
                raise
    else:
        log.warning("Stripe not found: id=%s", kwargs['sid'])
        return JSONRPCError(-1003, "Stripeid not found")

    return ""
示例#2
0
def hsv(server, hsv):
    hsv = spectra.hsv(int(hsv[0]), int(hsv[1]), int(hsv[2])).to("hsv").values

    result = requests.post(server + "/state",
                           json={
                               "hue": hsv[0],
                               "saturation": hsv[1],
                               "value": hsv[2]
                           })

    if result.status_code == 200:
        click.secho("Success!", fg="green")
        click.echo("Changed color to HSV: {}. Status Code: {}".format(
            hsv, result.status_code))
    else:
        click.secho("Requests Error!", blink=True, fg="red")
        click.echo("Status Code: {} Message: {}".format(
            result.status_code,
            result.json()["error"]))
示例#3
0
async def color_props(e):
    params = e.pattern_match.group(1) or ""
    args, color = parse_arguments(params, ['format', 'extended'])
    reply_message = await e.get_reply_message()

    if not color:
        await e.edit("Please provide a color...", delete_in=3)
        return

    if args.get('format') == 'rgb':
        r, g, b = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.rgb(r, g, b)
    elif args.get('format') == 'lab':
        l, a, b = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.lab(l, a, b)
    elif args.get('format') == 'lch':
        l, c, h = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.lch(l, c, h)
    elif args.get('format') == 'hsl':
        h, s, l = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.hsl(h, s, l)
    elif args.get('format') == 'hsv':
        h, s, v = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.hsv(h, s, v)
    elif args.get('format') == 'xyz':
        x, y, z = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.xyz(x, y, z)
    elif args.get('format') == 'cmy':
        c, m, y = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.cmy(c, m, y)
    elif args.get('format') == 'cmyk':
        c, m, y, k = re.findall(r'[\-.0-9]+', color)
        parsed = spectra.cmyk(c, m, y, k)
    else:
        parsed = spectra.html(color)

    rgb = [round(x * 255) for x in parsed.to('rgb').clamped_rgb]
    hsl = parsed.to('hsl').values
    hsv = parsed.to('hsv').values

    formats = {
        'hex': parsed.hexcode,
        'rgb': values__to_str(rgb),
        'hsl': values__to_str(hsl),
        'hsv': values__to_str(hsv)
    }

    if args.get('extended'):
        formats.update({
            'lab': values__to_str(parsed.to('lab').values),
            'lch': values__to_str(parsed.to('lch').values),
            'xyz': values__to_str(parsed.to('xyz').values),
            'cmyk': values__to_str(parsed.to('cmyk').values)
        })

    message = ""
    for fmt in formats.items():
        message += f"**{fmt[0]}**: `{fmt[1]}` \n"

    swatch = make_swatch(tuple(rgb))
    await e.delete()
    await e.client.send_file(e.chat_id,
                             swatch,
                             caption=message,
                             reply_to=reply_message)