2.1.1 • Published 2 years ago

rangarang v2.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

🌈 Rangarang

Rangarang (Persian word for "Colorful") is a tiny library that chooses the best colors from a picture. It extracts a beautiful background color, and an accessible text color.

It is:

  • Very, very lightweight ⚡️
  • Fast 🤔
  • TypeScript-friendly ✅
  • Works in both Node.js and broswer environments 🌍
  • Accessible: The colors have a >= 4.5 contrast ratio when used together!

Usage

This library uses the pixel data of the image. In browsers, it's easy to gather the pixel data using a <canvas> element:

import rangarang, { getDataFromCanvas } from 'rangarang';

const img = document.querySelector('img');
img.onload = () => {
  const { background, text } = rangarang(getDataFromCanvas(img)));
  console.log(background, text) // prints two hexacodes
};

But in Node.js environments, it's a bit harder. This library does not have any functions to extract pixel data from an image in Node, but you can extract it using your favorite library and use it:

const rangarang = require('rangarang').default;
rangarang(somehowGetPixelData());

Documentation

rangarang

Argument nameTypeDescription
dataUint8Array \| Uint8ClampedArrayImage's pixel data.
optionsobject?Options. 👇
options.skipPixelsnumber?Number of pixels to skip in each iteration. Defaults to 0
options.minLuminancenumber?The minimum luminance for an acceptable color. Defaults to 0.2
options.maxLuminancenumber?The maximum luminance for an acceptable color. Defaults to 0.8
options.minSaturationnumber?The maximum saturation for an acceptable color. Defaults to 0.1

getDataFromCanvas

Argument nameTypeDescription
imageHTMLImageElementThe source image.
sizenumber \| (originalWidth: number) => numberNumber or function to determine the <canvas>'s size. Bigger number/output results in a faster and less precise operation. Defaults to x => Math.max(80, x / 16)

Development

You can visually test the code:

npm run serve
npm run build -- --watch

Then head to localhost:8080/examples/web.

Todos

  • Better support for Node.js environments
  • Choose not only one color but more colors (secondary, background, etc.)
  • Allow to modify the behaviour of the code
  • Improve the performance
  • Write tests
2.1.1

2 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago