From fd96b54ccdf6e6670fc4e2cea31acebf1d2c45d9 Mon Sep 17 00:00:00 2001 From: blob8108 Date: Thu, 18 Dec 2014 18:34:54 +0000 Subject: Add color input support Fixes #29 --- blockext/blocks.py | 2 ++ blockext/generate.py | 1 + blockext/helper.py | 14 ++++++++++++++ example.py | 5 +++++ 4 files changed, 22 insertions(+) diff --git a/blockext/blocks.py b/blockext/blocks.py index a58bafc..12276af 100644 --- a/blockext/blocks.py +++ b/blockext/blocks.py @@ -84,6 +84,7 @@ class Input(object): * ``'boolean'`` -- boolean input (pointy ends) * ``'readonly-menu'`` -- menu input * ``'number-menu'`` -- editable number input with menu + * ``'color'`` -- color input with picker """ @@ -131,6 +132,7 @@ INPUT_SPECS = { "b": "boolean", "m": "readonly-menu", "d": "number-menu", + "c": "color", } diff --git a/blockext/generate.py b/blockext/generate.py index 5e7f296..207163b 100644 --- a/blockext/generate.py +++ b/blockext/generate.py @@ -78,6 +78,7 @@ INPUT_SELECTORS = { "boolean": "b", "readonly-menu": "txt", "number-menu": "n", + "color": "clr", } class SnapProgram(Program): diff --git a/blockext/helper.py b/blockext/helper.py index 4d866a6..db4fd39 100644 --- a/blockext/helper.py +++ b/blockext/helper.py @@ -8,6 +8,8 @@ from __future__ import (absolute_import, division, from future.builtins import * import itertools +import re +import struct from .blocks import Block from .server import Server, Response, NotFound, Download, Redirect, quote @@ -164,6 +166,18 @@ def decode_arg(arg, input_): arg = 0 elif input_.shape == "boolean": arg = True if arg == "true" else False if arg == "false" else None + elif input_.shape == "color": + try: + v = int(arg) + a, r, g, b = struct.unpack('BBBB', struct.pack('>i', v)) + arg = r, g, b + except (ValueError, struct.error): + color_pat = re.compile(r'^rgba\(([0-9]+),([0-9]+),([0-9]+),1\)$') + m = color_pat.match(arg) + if m: + arg = tuple(map(int, m.groups())) + else: + arg = (0, 0, 0) return arg diff --git a/example.py b/example.py index 0eec225..3b3690b 100644 --- a/example.py +++ b/example.py @@ -67,6 +67,11 @@ class Example: def x(self): pass + @command("set color to %c") + def set_color(self, c): + print(c) + + descriptor = Descriptor( name = "Fancy Spaceship", port = 1234, -- cgit v1.3.1