From 3a9f4edcff4ae37490a7e141c734fc19609da15f Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 24 Jun 2014 06:58:02 -0700 Subject: [PATCH] colors.lch: implement CIELCH colors. --- extra/colors/lch/authors.txt | 1 + extra/colors/lch/lch-tests.factor | 31 ++++++++++ extra/colors/lch/lch.factor | 95 +++++++++++++++++++++++++++++++ extra/colors/lch/summary.txt | 1 + 4 files changed, 128 insertions(+) create mode 100644 extra/colors/lch/authors.txt create mode 100644 extra/colors/lch/lch-tests.factor create mode 100644 extra/colors/lch/lch.factor create mode 100644 extra/colors/lch/summary.txt diff --git a/extra/colors/lch/authors.txt b/extra/colors/lch/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/colors/lch/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/colors/lch/lch-tests.factor b/extra/colors/lch/lch-tests.factor new file mode 100644 index 0000000000..9717a6f762 --- /dev/null +++ b/extra/colors/lch/lch-tests.factor @@ -0,0 +1,31 @@ +! Copyright (C) 2014 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: arrays colors kernel locals math.functions math.ranges +sequences tools.test ; + +IN: colors.lch + +{ t } [ + 0.0 1.0 0.1 [| r | + 0.0 1.0 0.1 [| g | + 0.0 1.0 0.1 [| b | + r g b 1.0 dup >LCHuv >rgba + [ >rgba-components 4array ] bi@ + [ 0.00001 ~ ] 2all? + ] all? + ] all? + ] all? +] unit-test + +{ t } [ + 0.0 1.0 0.1 [| r | + 0.0 1.0 0.1 [| g | + 0.0 1.0 0.1 [| b | + r g b 1.0 dup >LCHab >rgba + [ >rgba-components 4array ] bi@ + [ 0.00001 ~ ] 2all? + ] all? + ] all? + ] all? +] unit-test diff --git a/extra/colors/lch/lch.factor b/extra/colors/lch/lch.factor new file mode 100644 index 0000000000..d09e1fb213 --- /dev/null +++ b/extra/colors/lch/lch.factor @@ -0,0 +1,95 @@ +! Copyright (C) 2014 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: accessors colors colors.lab colors.luv colors.xyz kernel +locals math math.constants math.functions math.libm ; + +IN: colors.lch + +TUPLE: LCHuv l c h alpha ; + +C: LCHuv + +rad ( degrees -- radians ) + pi * 180 / ; inline + +: rad>deg ( radians -- degrees ) + 180 * pi / ; inline + +PRIVATE> + +M: LCHuv >rgba >luva >rgba ; + +M: LCHuv >xyza >luva >xyza ; + +M: LCHuv >luva + [ + [let + [ l>> ] [ c>> ] [ h>> ] tri :> ( l c h ) + h deg>rad :> hr + + l + c hr cos * + c hr sin * + ] + ] [ alpha>> ] bi ; + +GENERIC: >LCHuv ( color -- LCHuv ) + +M: object >LCHuv >luva >LCHuv ; + +M: LCHuv >LCHuv ; inline + +M: luva >LCHuv + [ + [let + [ l>> ] [ u>> ] [ v>> ] tri :> ( l u v ) + v u fatan2 rad>deg + [ dup 360 > ] [ 360 - ] while + [ dup 0 < ] [ 360 + ] while :> h + + l + u sq v sq + sqrt + h + ] + ] [ alpha>> ] bi ; + +TUPLE: LCHab l c h alpha ; + +C: LCHab + +M: LCHab >rgba >laba >rgba ; + +M: LCHab >laba + [ + [let + [ l>> ] [ c>> ] [ h>> ] tri :> ( l c h ) + h deg>rad :> hr + + l + c hr cos * + c hr sin * + ] + ] [ alpha>> ] bi ; + +GENERIC: >LCHab ( color -- LCHab ) + +M: object >LCHab >laba >LCHab ; + +M: LCHab >LCHab ; inline + +M: laba >LCHab + [ + [let + [ l>> ] [ a>> ] [ b>> ] tri :> ( l a b ) + b a fatan2 rad>deg + [ dup 360 > ] [ 360 - ] while + [ dup 0 < ] [ 360 + ] while :> h + + l + a sq b sq + sqrt + h + ] + ] [ alpha>> ] bi ; diff --git a/extra/colors/lch/summary.txt b/extra/colors/lch/summary.txt new file mode 100644 index 0000000000..bbfed8a708 --- /dev/null +++ b/extra/colors/lch/summary.txt @@ -0,0 +1 @@ +CIELCH colors