ReScript bindings for TinyColor: fast, small color manipulation and conversion. See also https://tinycolor.vercel.app
npm install rescript-tinycolor
Then add rescript-tinycolor as a dependency to rescript.json:
{
"dependencies": [
+ "rescript-tinycolor"
]
}open RescriptTinycolor
let redString = TinyColor.makeFromString("red")
// New instance made by name 'red'
let blueRgb = TinyColor.makeFromRgb({r: 0, g: 0, b: 255})
// New instance made with RGB values
let yellowHsl = TinyColor.makeFromHsl({h: 60, s: 0.94, l: 0.5})
// New instance made with HSL values (saturation and lightness must be given as percent fractions)
let darkGreenHsv = TinyColor.makeFromHsv({h: 100, s: 1.0, v: 0.3})
// New instance made with HSV values (saturation and value must be given as percent fractions)
let blueRgbWithAlpha = Option.map(blueRgb, TinyColor.setAlpha(0.2))
// New instance with changed alpha
let brightness = Option.map(redString, TinyColor.getBrightness)
// Some(76.245)
let hexString = Option.map(blueRgb, TinyColor.toHexString)
// Some("#0000ff")
let shadedBlue = Option.map(blueRgb, TinyColor.shade(~value=50))
// New instance with color shaded 50%
let isReadableInCombination = switch (redString, blueRgb) {
| (Some(red), Some(blue)) => TinyColor.isReadable(red, blue)
| _ => false
}
// returns a bool telling whether these colors can be used for background/textSee all available functions in the original TinyColor repo and example usage of all functions in the tests.
The generated API reference includes searchable signatures and links back to their ReScript source.
To build it locally, run npm run docs:build and open docs-site/index.html.
- It is not possible to create an invalid tinycolor instance, it will either return
Some(t)if it is valid, orNoneif it is invalid. E.g. an invalid instance can occur if you create a color with a string not corresponding to a valid color (beautifulRedis not a valid color) or you provide RGB values outside the valid range (0-255). - When creating instances with HSL and HSV values: saturation, lightness and value must be given as fractions instead of percent (
0.2 == 20%). - All functions accept only TinyColor-instances created by one of the
make--functions (orrandom()), it is not possible to pass in a string or RGB-record for the functions (which is possible in the original library). setAlpha(val)is immutable, it will return a new instance with changed alpha value (the other methods that modify a color (spin,lighten, etc.) is immutable from the original library).toName()returns an option, eitherSome(string)if a name could be deduced (e.g. red) orNoneif not.mostReadable()returns an option because an empty candidate list without fallback colors returnsNone.- To get multiple random colors with the
countparameter, the functionrandomMultiple()must be used (which is the same asrandom()only that it returns an array with lengthcountinstead of a single color).
If you find bugs or there are updates in TinyColor, feel free to open an issue or PR.
If you add, remove or change bindings, remember to update the tests as well. It should be at least one test for every binding. Run the tests with npm test from project root.
ReScript packages:
- rescript-polished - ReScript bindings for polished, including color manipulation and contrast helpers.
- re-color-contrast - ReScript implementation of WCAG color contrast calculation, similar to
readability(..)in this library.
JavaScript and TypeScript libraries that can be used through ReScript bindings:
- colord - small, immutable, chainable library with plugins for additional color spaces and features.
- color2k - small color parsing and manipulation library focused on bundle size.
- @colordx/core - modern library with OKLCH and OKLab support, plus plugins for spaces such as Display-P3.
- chroma-js - color conversion, interpolation, and scales, particularly useful for data visualization.
- color - immutable color parsing, conversion, and manipulation with CSS color string support.
- Color.js - comprehensive modern color spaces, gamut mapping, and color-difference algorithms from editors of the CSS Color specifications.
- Culori - modular color conversion, interpolation, blending, and color-difference functions across many color spaces.
- @texel/color - tree-shakeable color conversion and gamut mapping for graphics, creative coding, and wide-gamut color spaces.