]> gitweb.factorcode.org Git - factor.git/commitdiff
colors.hsl: adding HSL color support.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 22 Oct 2012 22:19:51 +0000 (15:19 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 22 Oct 2012 22:19:51 +0000 (15:19 -0700)
extra/colors/hsl/authors.txt [new file with mode: 0644]
extra/colors/hsl/hsl-docs.factor [new file with mode: 0644]
extra/colors/hsl/hsl.factor [new file with mode: 0644]
extra/colors/hsl/summary.txt [new file with mode: 0644]

diff --git a/extra/colors/hsl/authors.txt b/extra/colors/hsl/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/colors/hsl/hsl-docs.factor b/extra/colors/hsl/hsl-docs.factor
new file mode 100644 (file)
index 0000000..38eedc6
--- /dev/null
@@ -0,0 +1,14 @@
+USING: help.markup help.syntax ;
+IN: colors.hsl
+
+HELP: hsla
+{ $class-description "The class of HSL (Hue, Saturation, Lightness) colors with an alpha channel. All slots store values in the interval " { $snippet "[0,1]" } "." } ;
+
+ARTICLE: "colors.hsl" "HSL colors"
+"THe " { $vocab-link "colors.hsl" } " vocabulary implements colors specified by their hue, saturation, and lightness components, together with an alpha channel."
+{ $subsections
+    hsla
+    <hsla>
+    rgba>hsla
+}
+{ $see-also "colors" } ;
diff --git a/extra/colors/hsl/hsl.factor b/extra/colors/hsl/hsl.factor
new file mode 100644 (file)
index 0000000..90478df
--- /dev/null
@@ -0,0 +1,64 @@
+! Copyright (C) 2012 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors colors combinators kernel locals math
+math.order ;
+
+IN: colors.hsl
+
+TUPLE: hsla < color
+{ hue read-only }
+{ saturation read-only }
+{ lightness read-only }
+{ alpha read-only } ;
+
+C: <hsla> hsla
+
+<PRIVATE
+
+: value ( p q t -- value )
+    dup 0 < [ 1.0 + ] when
+    dup 1 > [ 1.0 - ] when
+    {
+        { [ dup 1/6 < ] [ [ over - ] dip * 6 * + ] }
+        { [ dup 1/2 < ] [ drop nip ] }
+        { [ dup 2/3 < ] [ [ over - ] dip 2/3 swap - * 6 * + ] }
+        [ 2drop ]
+    } cond ;
+
+PRIVATE>
+
+M: hsla >rgba
+    {
+        [ hue>> ] [ saturation>> ] [ lightness>> ] [ alpha>> ]
+    } cleave [| h s l |
+        s zero? [
+            l l l
+        ] [
+            l 0.5 < [ l s 1 + * ] [ l s + l s * - ] if :> q
+            l 2 * q - :> p
+            p q h 1/3 + value
+            p q h value
+            p q h 1/3 - value
+        ] if
+    ] dip <rgba> ;
+
+: rgba>hsla ( rgba -- hsla )
+    >rgba-components [| r g b |
+        r g b min min :> min-c
+        r g b max max :> max-c
+        min-c max-c + 2 / :> l
+        max-c min-c - :> d
+        d zero? [ 0.0 0.0 ] [
+            max-c {
+                { r [ g b - d / g b < 6.0 0.0 ? + ] }
+                { g [ b r - d / 2.0 + ] }
+                { b [ r g - d / 4.0 + ] }
+            } case 6.0 /
+            l 0.5 > [
+                d 2 max-c - min-c - /
+            ] [
+                d max-c min-c + /
+            ] if
+        ] if l
+    ] dip <hsla> ;
diff --git a/extra/colors/hsl/summary.txt b/extra/colors/hsl/summary.txt
new file mode 100644 (file)
index 0000000..063c135
--- /dev/null
@@ -0,0 +1 @@
+HSL colors