]> gitweb.factorcode.org Git - factor.git/commitdiff
colors.hex: support varying length hex notations.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 27 Nov 2016 05:13:12 +0000 (21:13 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 27 Nov 2016 05:13:12 +0000 (21:13 -0800)
6 characters: rrggbb (1.0 alpha)
8 characters: rrggbbaa
3 characters: rgb (1.0 alpha)
4 characters: rgba

basis/colors/hex/hex-tests.factor
basis/colors/hex/hex.factor

index 94df043e3dd0aaca3f46508d99db6bbfbfffa97f..bd79f3e1749c8d8ab724264d607b9b7c00ad9cf0 100644 (file)
@@ -7,3 +7,13 @@ USING: colors colors.hex tools.test ;
 { HEXCOLOR: abcdef } [ "abcdef" hex>rgba ] unit-test
 { HEXCOLOR: abcdef } [ "ABCDEF" hex>rgba ] unit-test
 { "ABCDEF" } [ HEXCOLOR: abcdef rgba>hex ] unit-test
+
+{ HEXCOLOR: 00000000 } [ 0.0 0.0 0.0 0.0 <rgba> ] unit-test
+{ HEXCOLOR: FF000000 } [ 1.0 0.0 0.0 0.0 <rgba> ] unit-test
+{ HEXCOLOR: FFFF0000 } [ 1.0 1.0 0.0 0.0 <rgba> ] unit-test
+{ HEXCOLOR: FFFFFF00 } [ 1.0 1.0 1.0 0.0 <rgba> ] unit-test
+{ HEXCOLOR: FFFFFFFF } [ 1.0 1.0 1.0 1.0 <rgba> ] unit-test
+
+{ HEXCOLOR: cafebabe } [ "cafebabe" hex>rgba ] unit-test
+{ HEXCOLOR: 112233 } [ "123" hex>rgba ] unit-test
+{ HEXCOLOR: 11223344 } [ "1234" hex>rgba ] unit-test
index c9f13b7236fe6e7c465d80e6b6f1952317a8660f..ba42206a30ac32368b30b4f9f39d29d20128757f 100644 (file)
@@ -1,13 +1,18 @@
 ! Copyright (C) 2010 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: accessors colors formatting grouping kernel lexer math
-math.parser sequences ;
+USING: accessors colors combinators formatting grouping kernel
+lexer math math.parser sequences ;
 
 IN: colors.hex
 
 : hex>rgba ( hex -- rgba )
-    2 group [ hex> 255 /f ] map first3 1.0 <rgba> ;
+    dup length {
+        { 6 [ 2 group [ hex> 255 /f ] map first3 1.0 ] }
+        { 8 [ 2 group [ hex> 255 /f ] map first4 ] }
+        { 3 [ [ digit> 15 /f ] { } map-as first3 1.0 ] }
+        { 4 [ [ digit> 15 /f ] { } map-as first4 ] }
+    } case <rgba> ;
 
 : rgba>hex ( rgba -- hex )
     [ red>> ] [ green>> ] [ blue>> ] tri