M README +13 -4
@@ 1,12 1,21 @@
Showcolours
-==========
+===========
Read a list of colours from the command-line, and display them in the browser.
Invocation:
# red and light green
- ./showcolours.py rgb 255 0 0 128 255 128 > tmp.html && xdg-open tmp.html
+ ./showcolours.py rgb 255 0 0 128 255 128 > tmp.html && xdg-open tmp.html
+
+ # red and light green
+ ./showcolours.py rgb 1 0 0 0.5 1 0.5 > tmp.html && xdg-open tmp.html
+
+ # red and blue
+ ./showcolours.py rgb 1 0 0 0 0 1. > tmp.html && xdg-open tmp.html
+
+ # nearly black
+ ./showcolours.py rgb 1 0 0 0 0 1 > tmp.html && xdg-open tmp.html
# red and light green again
./showcolours.py hex ff0000 '#88ff88' > tmp.html && xdg-open tmp.html
@@ 23,9 32,9 @@ Run the manual-inspection testrun like s
Sketch of future features
-------------------------
-- [ ] rgb:
+- [x] rgb:
- - [ ] interpret as fractions if there is a decimal number anywhere in the
+ - [x] interpret as fractions if there is a decimal number anywhere in the
args
showcolour rgb 0.1 0.8 0.1 0.2 0.2 0.9
showcolour rgb 0 1 0 1 1. 0
M showcolours.py +8 -2
@@ 113,7 113,7 @@ def colours_from_rgb1_numbers(numbers):
Colour and/or InvalidColour objects.
"""
for chunk in chunks(numbers, 3):
- if len(chunk) == 3 and all(map(can_only_be_decimal, chunk)):
+ if len(chunk) == 3 and all(map(can_also_be_decimal, chunk)):
yield Colour(rgb=map(float, chunk))
continue
else:
@@ 180,8 180,14 @@ def can_also_be_decimal(number):
def interpret_colours(numbers, hint):
+ """Given a list of numbers and an interpretation hint, invoke the correct
+ parser on the numbers and return the result.
+ """
if hint == 'rgb':
- return colours_from_rgb255_numbers(numbers)
+ if any(map(can_only_be_decimal, numbers)):
+ return colours_from_rgb1_numbers(numbers)
+ else:
+ return colours_from_rgb255_numbers(numbers)
if hint == 'hex':
return colours_from_hex_numbers(numbers)
M test_showcolours.py +29 -0
@@ 90,6 90,35 @@ class Test_interpret_colours():
]
assert result == expected
+ def test_it_interprets_0s_and_1s_as_rgb255(self):
+ """If I pass a collection of 0 and 1 triplets without any dots, and
+ hint that they should be interpreted as rgb, interpret them as
+ rgb255"""
+ result = list(sc.interpret_colours(
+ numbers=['0', '1', '1', '0', '0', '1'],
+ hint='rgb'
+ ))
+ expected = [
+ sc.Colour(rgb=(0, 1 / 255.0, 1 / 255.0)),
+ sc.Colour(rgb=(0, 0, 1 / 255.0)),
+ ]
+ assert result == expected
+
+ def test_it_interprets_0s_and_1s_with_a_dot_as_rgb(self):
+ """If I pass a collection of 0 and 1 triplets without any dots, and
+ hint that they should be interpreted as rgb, interpret them as
+ rgb255"""
+ result = list(sc.interpret_colours(
+ numbers=['0', '1', '1', '0', '0', '1.'],
+ hint='rgb'
+ ))
+ expected = [
+ sc.Colour(rgb=(0, 1, 1)),
+ sc.Colour(rgb=(0, 0, 1)),
+ ]
+ assert result == expected
+
+
class Test_colours_from_hex_numbers():
"""Test the colours_from_hex_numbers function"""
M testrun_showcolours.sh +6 -2
@@ 1,10 1,14 @@
-temp_rgb255=$(tempfile --prefix=col- --suffix=.html)
+temp_rgb255=$(tempfile --prefix=col- --suffix=-rgb255.html)
./showcolours.py rgb 255 128 0 \
0 128 255 \
255 0 128 \
128 0 255 > $temp_rgb255
gnome-open $temp_rgb255
-temp_hex=$(tempfile --prefix=col- --suffix=.html)
+temp_rgb1=$(tempfile --prefix=col- --suffix=-rgb1.html)
+./showcolours.py rgb 0. 0 1 1 0 0 > $temp_rgb1
+gnome-open $temp_rgb1
+
+temp_hex=$(tempfile --prefix=col- --suffix=-hex.html)
./showcolours.py hex '#19af32' 5588dd 554433 > $temp_hex
gnome-open $temp_hex