Skip to content

Instantly share code, notes, and snippets.

@mfornos
Last active August 29, 2015 13:57
Show Gist options
  • Save mfornos/9750408 to your computer and use it in GitHub Desktop.
Save mfornos/9750408 to your computer and use it in GitHub Desktop.
RYB Color Model LESS Mixins
/*
----------------------
RYB Color Model Mixins
----------------------
RYB is an artistic color model that provides somewhat better color harmonies
(complementary, triadic &c), although not mathematically perfect since
cannot cover the whole visible spectrum. Neither good for physical pigments encoding.
http://en.wikipedia.org/wiki/RYB_color_model
*/
/*
Color wheels
*/
@ryb-wheel: 0, 26, 52, 83, 120, 130, 141, 151, 162, 177, 190, 204, 218, 232, 246,
261, 275, 288, 303, 317, 330, 338, 345, 352, 360;
@rgb-wheel: 0, 8, 17, 26, 34, 41, 48, 54, 60, 81, 103, 123, 138, 155, 171, 187,
204, 219, 234, 251, 267, 282, 298, 329, 360;
/*
Maps a given color on the RGB wheel to Itten's RYB wheel.
@c - a color
Sets the @ryb variable as result color.
*/
.rgb2ryb(@c)
{
.map-hue(hue(@c), @ryb-wheel);
@ryb: hsl(@_, saturation(@c), lightness(@c), alpha(@c));
}
/*
Maps a given color on Itten's RYB wheel to RGB wheel.
@c - a color
Sets the @ryb variable as result color.
*/
.ryb2rgb(@c)
{
.map-hue(hue(@c), @rgb-wheel);
@rgb: hsl(@_, saturation(@c), lightness(@c), alpha(@c));
}
/*
Maps the given hue to the specified color wheel.
@hue - a color hue
@wheel - the color wheel to be approximated
Sets the @_ variable as result hue.
*/
.map-hue(@hue, @wheel)
{
@d: mod(15, @hue);
@i: ceil(@hue / 15);
@x0: extract(@wheel, @i + 1);
@x1: extract(@wheel, min(@i + 2, 25)); // min for index boxing
@_: @x0 + (@x1 - @x0) * max(0, @d / 15); // max is needed to coerce some edge cases
}
/*
Approximates the complementary of a given color using Itten's RYB wheel.
@c - a color
Sets the @_ variable as result color.
*/
.complementary(@c)
{
.rgb2ryb(@c);
.ryb2rgb(spin(@ryb, 180));
@_: @rgb;
}
/*
Example of use
*/
.my-complementary
{
background: @_;.complementary(orange);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment