rybitten
v0.29.0
Published
A color space conversion library for transforming between RGB and RYB colors.
Maintainers
Readme
RYBitten 🔴🟡🔵
A color space conversion library for transforming between RGB and RYB (Red-Yellow-Blue) colors.
What is RYBitten?
RYBitten is a lightweight library for translating colors between RGB and a custom RYB (Red-Yellow-Blue) color space. It’s designed for developers, generative artists, and designers who want to create harmonious, consistent, or randomized color palettes effortlessly. The library emulates Johannes Itten's chromatic circle using trilinear interpolation and customizable options, making it a versatile tool for creative projects.
"Play becomes joy, joy becomes work, work becomes play." Johannes Itten, Bauhaus
Features ✨
- Color Conversion: Effortlessly translate between RGB and a custom RYB space.
- Easy Integration: A tiny library with no dependencies.
- Customizable Gamuts: Experiment with historical color spaces or create your own.
- Subtractive Color Model: Emulates subtractive color.
Installation 📦
Install RYBitten with your favorite package manager:
npm install rybittenOr include it directly in your HTML:
<script type="module">
import { ryb2rgb, rybHsl2rgb } from "https://esm.sh/rybitten/";
import { cubes } from "https://esm.sh/rybitten/cubes"; // Optional gamut presets
</script>Include RYBitten in your project
// ES6 style
import { ryb2rgb } from 'rybitten';
// CommonJS style
const { ryb2rgb } = require('rybitten');Usage in Browser / p5.js (UMD)
For projects that don't use a bundler (like p5.js sketches), you can use the UMD build. This exposes a global rybitten object.
<script src="https://unpkg.com/rybitten"></script>
<script>
function setup() {
createCanvas(400, 400);
noLoop();
}
function draw() {
// Access functions via the global 'rybitten' object
const rgb = rybitten.ryb2rgb([0, 1, 0]);
background(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
}
</script>p5.js Color Mode Extension
RYBitten includes a special p5.js extension that adds native RYB color mode support, just like RGB, HSB, or HSL. The extension automatically initializes when you include the p5-specific UMD build:
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>
<script src="https://unpkg.com/rybitten/dist/p5.rybitten.umd.js"></script>
<script>
function setup() {
createCanvas(400, 400);
// Use RYB color mode just like RGB or HSL!
colorMode(RYB);
background(255, 0, 0); // Red in RYB space
fill(0, 255, 0); // Yellow in RYB space
circle(200, 200, 100);
}
</script>Available Color Modes:
RYB- Red-Yellow-Blue color mode (0-255 range by default)RYBHSL- RYB with Hue-Saturation-Lightness (360, 100, 100 range by default)
Using Custom Color Cubes in p5.js:
function setup() {
createCanvas(400, 400);
// Use a custom historical color cube
// Note: The UMD build does not export the cubes collection.
// Copy the cube values you want to use directly in your code.
colorMode(rybhsl([
[233/255, 199/255, 173/255], // White
[214/255, 76/255, 127/255], // Red
[238/255, 204/255, 124/255], // Yellow
[230/255, 174/255, 115/255], // Orange
[86/255, 141/255, 146/255], // Blue
[118/255, 83/255, 97/255], // Violet
[196/255, 192/255, 118/255], // Green
[60/255, 52/255, 40/255] // Black
]));
fill(0, 100, 50); // Hue 0, full saturation, mid lightness
rect(0, 0, 200, 200);
}The extension works with both p5.js 1.x and 2.0, and supports both global and instance modes.
Quick Start 🚀
All RGB and RYB values are in the range [0, 1].
import { ryb2rgb } from 'rybitten';
const rgbColor = ryb2rgb([1, 0, 0.5]);
console.log(rgbColor); // Outputs the RGB equivalentAPI Reference 📖
ryb2rgb(coords: ColorCoords, {cube?: ColorCube = RYB_ITTEN, easingFn? = easingSmoothstep}): ColorCoords
Convert RYB to RGB using trilinear interpolation.
coords:[0…1, 0…1, 0…1]RYB coordinatesoptions: (optional) An object with the following properties:cube: (optional): See the note on the color cube below defaults toRYB_ITTENeasingFn: (optional) Custom easing function used for the interpolation, defaults tosmoothstep
@return:[0…1, 0…1, 0…1]RGB coordinates
Note: RYB uses a subtractive color model where black = all colors, white = no colors. white will turn to black, and black will turn to white.
rybHsl2rgb(hsl: [h: number, s: number, l: number], options?): ColorCoords
Convert HSL to RGB, then apply the RYB space.
hsl: Array of[hue (0…360), saturation (0…1), lightness (0…1)]options: (optional) An object with the following properties:cube: (optional) See the note on the color cube beloweasingFn: (optional) A custom easing function for the interpolation, defaults tosmoothstepinvertLightness: (optional) Inverts the lightness value, defaults totrue(0 is black, 1 is white), if set tofalsel:0 is white, l:1 is black
@return:[0…1, 0…1, 0…1]RGB coordinates
Converts HSL coordinates to RGB, then translates them to the custom RYB color space using ryb2rgb. The HSL coordinates are in the range [0,360], [0, 1], [0, 1]. Lightness is inverted to match the RYB color space.
Interpolation Color Cube 🎛️
The default RYB color cube used for interpolation in RYBitten is tuned to mimic Johannes Itten's chromatic circle. By adjusting the cube, you can achieve different effects and customize the RYB to RGB conversion.
The cube is inverted to match the subtractive color model, where white is the absence of color and black is the presence of all colors.
const RYB_CUBE = [
// White
[253 / 255, 246 / 255, 237 / 255],
// Red
[227 / 255, 36 / 255, 33 / 255],
// Yellow
[243 / 255, 230 / 255, 0],
// Orange
[240 / 255, 142 / 255, 28 / 255],
// Blue
[22 / 255, 153 / 255, 218 / 255],
// Violet
[120 / 255, 34 / 255, 170 / 255],
// Green
[0, 142 / 255, 91 / 255],
// Black
[29 / 255, 28 / 255, 28 / 255],
];Custom Gamuts Presets 🧊
The library ships with a curated list of color gamuts that you can use to experiment with different color spaces. The default gamut is based on the work of Johannes Itten. But you can access other gamuts by importing the CUBES map.
Each gamut is an object with the following properties:
title: The name of the color spacereference: A reference image used to pick the edges of the custom color gamutyear: The year the color space was introducedcube: The color cube used for interpolation
import { rybHsl2rgb } from 'rybitten';
import { cubes } from 'rybitten/cubes';
const { cube } = cubes.get('munsell');
console.log(cube);
/**
* [
* ...8 ColorCoords entries
* ]
*/
rybHsl2rgb([0, 1, 0.5], { cube });TypeScript Support 📝
RYBitten is written in TypeScript and includes type definitions out of the box:
import type { ColorCoords, ColorCube } from 'rybitten';Available Color Gamuts
The library provides a collection of historical and modern color gamuts through the cubes Map. Import and use them like this:
import { rybHsl2rgb } from 'rybitten';
import { cubes } from 'rybitten/cubes';
// Access any gamut by its key
const munsellCube = cubes.get('munsell').cube;
const albersCube = cubes.get('albers').cube;
// Use it in color conversion
const rgbColor = rybHsl2rgb([0, 1, 0.5], { cube: munsellCube });
// Get metadata about the color space
const { title, author, year, reference } = cubes.get('munsell');Historical Color Spaces
| Key | Title | Year | Reference | | --- | --- | --- | --- | | itten | Johannes Itten: Chromatic Circle | 1961 | reference | | itten-normalized | Johannes Itten: Chromatic Circle (Paper-white) | 1961 | reference | | itten-neutral | Nathan Gossett & Baoquan Chen: Paint Inspired Color Compositing | 2004 | reference | | bezold | Wilhelm von Bezold: Farbentafel | 1874 | reference | | boutet | Claude Boutet: Twelve-color color circles | 1708 | reference | | hett | J. A. H. Hett: RGV Color Wheel | 1908 | reference | | schiffermueller | Ignaz Schiffermüller: Versuch eines Farbensystems | 1772 | reference | | harris | Moses Harris: The Natural System of Colours | 1766 | reference | | harrisc82 | Moses Harris / C82: The Natural System of Colours | 1766 | reference | | harrisc82alt | Moses Harris / C82: The Natural System of Colours (alt) | 1766 | reference | | goethe | Goethe: Farbenkreis | 1809 | reference | | munsell | Munsell Color System | 1905 | reference | | munsell-alt | Cleland & Munsell: A Grammar of Color | 1921 | reference | | hayer | Charles Hayter: New Practical Treatise on the Three Primitive Colours | 1826 | reference | | bormann | Heinrich-Siegfried Bormann: Gouache tint study for Josef Alber's Preliminary Course" | 1931 | reference | | chevreul | Michel Eugène Chevreul: Chromatic Circle | 1839 | | | runge | Philipp Otto Runge: Farbenkugel | 1810 | reference | | maycock | Mark M. Maycock's "Scale of Normal Colors and their Hues" | 1895 | | | colorprinter | John Earhart's "The Color Printer" | 1892 | | | japschool | Japanese School Textbook | 1930 | reference | | kindergarten1890 | Milton Bradley's Kindergarten Occupation Material | 1890 | reference | | albers | Josef Albers: Interaction of Color | 1942 | reference | | lohse | Richard Paul Lohse's "Kunsthalle Bern Poster" | 1970 | reference | | marvel-news | Marvel Comics: 64 Color Chart on Newsprint | 1982 | reference | | apple80s | Apple: HyperCard User Manual | 1989 | reference | | apple90s | Apple: Macintosh Reference Manual | 1990 | reference | | rgb | James Clerk Maxwell's "Inverted RGB" | 1860 | |
Featured Artist Spectrum
The following color spaces were provided by artists and designers who have contributed to the project.
| Key | Title | Year | Reference | | --- | --- | --- | --- | | ippsketch | Ippsketch: imposter syndrome | 2022 | reference | | ten | Roni Kaufman's "Ten" | 2022 | reference | | pixelart | Tofu: Pixel Art | 2024 | reference | | ryan | Ryan: Compositions Palette | 2024 | reference | | clayton | Greg Clayton: Intrinsic Value Plate | 2017 | reference |
Utility Functions 🛠️
The library exports several utility functions that are used internally for color interpolation. But if you are anything like me, you might find them useful for other purposes when working with this library.
lerp(a: number, b: number, t: number): number
Linear interpolation between two values.
import { lerp } from 'rybitten';
lerp(0, 100, 0.5); // returns 50blerp(a00: number, a01: number, a10: number, a11: number, tx: number, ty: number): number
Bilinear interpolation between four points in a 2D space. Useful for interpolating values on a rectangular grid.
import { blerp } from 'rybitten';
// Interpolate between four corners of a unit square
blerp(0, 1, 1, 2, 0.5, 0.5); // returns center valuetrilerp(a000: number, a010: number, a100: number, a110: number, a001: number, a011: number, a101: number, a111: number, tx: number, ty: number, tz: number): number
Trilinear interpolation between eight points in a 3D space. This is the core interpolation function used for the color cube conversion.
import { trilerp } from 'rybitten';
// Interpolate within a color cube
const value = trilerp(
0, 1, 1, 1, // front face values
0, 1, 1, 1, // back face values
0.5, 0.5, 0.5 // position in cube
);hslToRgb(hsl: [h: number, s: number, l: number]): ColorCoords
Converts standard HSL color values to RGB. Adapted from Culori (MIT License).
hsl: Array of[hue (0…360), saturation (0…1), lightness (0…1)]@return:[0…1, 0…1, 0…1]RGB coordinates
import { hslToRgb } from 'rybitten';
hslToRgb([0, 1, 0.5]); // returns [1, 0, 0] (red)
hslToRgb([120, 1, 0.5]); // returns [0, 1, 0] (green)
hslToRgb([240, 1, 0.5]); // returns [0, 0, 1] (blue)License 📄
RYBitten is distributed under the MIT License.
