summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblob8108 <blob8108@gmail.com>2014-12-18 18:34:54 +0000
committerblob8108 <blob8108@gmail.com>2014-12-18 18:34:54 +0000
commitfd96b54ccdf6e6670fc4e2cea31acebf1d2c45d9 (patch)
treeccc5ee44bc31da8b381242c8d1f9d8e9ccd7b742
parent31666e21bc61ce6eb212c159780e02b10678ddc9 (diff)
downloadblockext-fd96b54ccdf6e6670fc4e2cea31acebf1d2c45d9.tar.gz
blockext-fd96b54ccdf6e6670fc4e2cea31acebf1d2c45d9.zip
Add color input support
Fixes #29
-rw-r--r--blockext/blocks.py2
-rw-r--r--blockext/generate.py1
-rw-r--r--blockext/helper.py14
-rw-r--r--example.py5
4 files changed, 22 insertions, 0 deletions
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,