Color manipulations
There is no dedicated Color
data type in rot.js; passing strings around is sufficient in most cases (such as ROT.Display::draw
). However, if you need to perform color adjustments and computations, it is suitable to represent them as arrays of numbers. For this case, the ROT.Color
namespace offers a bunch of useful methods.
Converting string → array
SHOW(
ROT.Color.fromString("rgb(10, 128, 230)"),
ROT.Color.fromString("#faa"),
ROT.Color.fromString("#83fcc4"),
ROT.Color.fromString("goldenrod")
);
Converting array → string
SHOW(
ROT.Color.toRGB([10, 128, 230]),
ROT.Color.toHex([10, 128, 230])
);
Converting between RGB and HSL
SHOW(
ROT.Color.rgb2hsl([51, 102, 51]),
ROT.Color.hsl2rgb([0.333, 0.333, 0.3])
);
Adding and mixing colors
These methods accept a variable number of arguments. Methods whose names end with an underscore modify their first argument instead of returning a new array. Note that color values are not clamped to 0..255; this happens only when serializing to a string.
SHOW( /* addition = lightening */
ROT.Color.add([10, 128, 230], [200, 10, 15], [30, 30, 100]),
ROT.Color.add_([10, 128, 230], [200, 10, 15])
);
SHOW( /* multiplication = darkening */
ROT.Color.multiply([10, 128, 230], [200, 10, 15]),
ROT.Color.multiply_([10, 128, 230], [200, 10, 15])
);
Interpolating between two colors
The third argument specifies the interpolation factor (0 = first color, 1 = last color, 0.5 = default = halfway between them).
SHOW(
/* computed in RGB space */
ROT.Color.interpolate([10, 128, 230], [30, 255, 255], 0.3),
/* computed in HSL space */
ROT.Color.interpolateHSL([10, 128, 230], [30, 255, 255], 0.3)
);
Creating random variants
You can create new colors by adjusting an exiting color by a (normally distributed) random value. Second argument is a set of standard deviations; note that ~95% of generated colors will fall within 2*stddev
.
SHOW(
ROT.Color.randomize([100, 128, 230], [30, 10, 20]),
ROT.Color.randomize([100, 128, 230], [30, 10, 20]),
ROT.Color.randomize([100, 128, 230], [30, 10, 20])
);