Single Color Methods of GTC

Whohoo release 1.7 (and 1.6) brought a thorough architectural rewrite, new color spaces (HSV, HWB, YIQ), new formats ([named] array, string and css_string), choosable value ranges, closed both open issues, and introduced the named argument API I wrote last time about. And as promised, this post is about the methods: distance, set, add and blend.

Color Properties

Yes GTC gives you color sets, but how do you know a color, from which you calculate the set, has all the right properties? Many of these properties you simply get by converting the values into color spaces like HSL or HWB. So:


$color->values( in => 'HSL', as => 'hash' )->{lightness};

will tell you how bright or dark the color is. At this point you might wish there is an method just to give you the lightness, but on other hand there are around 10 - 14 color spaces and do we really want 36 methods just for one purpose? Plus if you want to just get the cyan value, how I decide its from CMY or CMYK? So even its a bit more to write, its clear what your get when you write:

$color->values( in => 'CMYK', as => 'hash' )->{cyan};
($color->values( 'CMYK' ))[0]; # same thing but shorter

Because every color space (9 supported by next version) was constructed for a different need, we can provide this way a lot of properties. But all these characterise one color. To characterise color relations we have the method distance. Yes its just the Euclidean distance between two point in color space, as Pythagoras knew it. But as mentioned, having the ability to calculate it in different spaces already magnifies its power.

my $d = $color->distance( to => $other_color, in => 'HWB');

But our method has two additional arguments which makes it even more powerful. The argument select selects which dimensions distance observes. If you want in HSL only observe the hue you could say so:

$color->distance( to => $other, in => 'HSL', select => 'hue');

You could even weight the saturation double as much as the lightness:

$color->distance( to => $other, in => 'HSL', select => 'ssl');

The fourth argument, which is also available with the values method, allows you to change the value ranges of each color space dimension. For instance lets calculate very simply distances in RGB but not using the usual 0 .. 255 value ranges, but the normalized ones (0..1):

$color->distance( to => $other, range => 'normal');

This is especially important if you want to compare your distance with an value that is computed on godknowswhat space dimension sizes.

Related Colors

Beside the obvious constructor method new, there are 3 more methods that create an color object, which has a relation to the current object.

The simplest one of then is set. It replaces some values of the current object with the given. Its full power comes from the ability to accept values in all supported color space. This gives us the ability to create an brown with a certain brightness, but immediately output the values in another format.


color('brown')->set( brightness => 70 )->values( 'CMYK' );

In case you wonder, brightness is the name of the third axis in HSB color space, which is just a renamed version of HSV.

While set changes to an absolute value, add performs relative changes. To slightly dim a Pink we just need:


color('pink')->add( lightness => -10 );

The third method is blend which does something similar, but is way more powerful. While add allows changes along the axis of some color space, blend
lets you select an arbitrary point on a line, that is spanned by any two points (colors) in a color space. It takes three named argument from which only the first ('with') is required. It takes a color object or color definition of the second color to blend (or mix) with. The second argument is 'pos', telling the position on the just mentioned line you want to select. The default value for that is 0.5, giving you an 1:1 blend between the two colors. zero would be $self and one the second color, given by the 'with' argument. Your allowed to state even values beyond 0 .. 1. If the resulting color would be outside the defined border of that color space (default is HSL), it will be clamped to nearest values that produce a valid color (circular dimension will be of course rotated). The third argument is our well known buddy 'in', allowing you to choose the color space in which the calculation takes place.

$color->blend( with => 'grey', pos => 0.1); # we fade to gray

The next episode will be about the set creation methods, the heart of the module before I close up this mini series with musings about the internal architecture.

Leave a comment

About lichtkind

user-pic Kephra, Articles, Books, Perl, Programming