thx is really growing fast and since it is not yet documented I want to start giving users some hints on the classes included.
The thx.color package is the first set of features I will introduce since the API is pretty stable and I use it a lot.
Creating a color from its channels is really easy:
var color = new Rgb(255, 0, 0); // RGB values are integers from 0 to 255
Of course you can use alternatives to create a Rgb color:
color = Rgb.fromInt(0xFF000);
or
color = Rgb.fromFloats(1.0, 0.0, 0.0);
… but if you are as lazy as I am you can use:
color = NamedColors.red;
or even
color = Colors.parse("red");
Note that Colors.parse()
will try everything to find a matching color, infact you can pass a:
- color name, as defined in NamedColors; names with spaces are accepted aswell (ei: “
dark sea green
“) - css color:
#ff0000
- rgb definition:
rgb(255,0,1)
- hsl definition:
hsl(0,1,0.5)
- cmyk definition:
cmyk(0,1,1,0)
Other than Rgb you can define colors in the following colorspaces: Hsl
, Grey
, Cmyk
I find Hsl particularly useful when you want to create smooth interpolations:
using thx.colors.Hsl; // ... var midcolor = Hsl.interpolate(NamedColors.red.toHsl(), NamedColors.blue.toHsl(), 0.5);
If you want to recycle the interpolation and generate an array of colors you can use:
using Arrays;using thx.colors.Hsl; // ... var f = Hsl.interpolatef(NamedColors.red.toHsl(), NamedColors.blue.toHsl()), len = 10; var colors = Ints.range(len).map(function(_, i) return f(i / (len-1)));
The interpolate/interpolatef methods in Colors
will do the same but starting from string definitions.
This can be very handy when you want to interpolate a CSS color. The Rgb.contrast
/constrastBW
methods are also pretty useful. The former will try to find a pastel color that contrasts with the passed one while the latter will always return white or black. The algorithm of contrastBW
uses the grey equivalent of the passed color to estabilish the better contrast for the human vision.