
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
visually-random-color
Advanced tools
A simple, lightweight package with no dependencies to generate a random color with as little visual skew as possible
visually-random-color is a JavaScript library designed to produce visually uniform random colors with minimal bias.
The goal of this package is to generate colors that appear evenly distributed across the color space, avoiding biases towards darker or lighter shades. Naive approaches such as choosing uniformly 0-255 values for RGB colors can lead to a dark skew due to the geometry of RGB's color space.
visually-random-color utilises the CEILAB color space. This space is chosen for its perceptual uniformity, ensuring that colors are generated in a way that provides consistent visual differentiation and keeps color skew as low as possible.
Colors are generated randomly in Lab format, and the package also offers functionality to convert these colors to more common color spaces such as RGB, enabling seamless integration into various applications and environments.
Conversion algorithms sourced from http://www.brucelindbloom.com/, and were implemented from scratch.
You can install visually-random-color via npm:
npm install visually-random-color
const {
randRGB,
randRGB255,
randLab,
randXYZ,
} = require("visually-random-color");
// Generate a random RGB color with an array of values scaled to match the nominal 0-1 range
const randomRGB = randRGB();
console.log(randomRGB);
// Generate a random RGB color with an array of values scaled by a factor of 255
const randomRGB255 = randRGB255();
console.log(randomRGB255);
// Generate a random LAB color as an array of values, guaranteed to be in the nominal ranges
const randomLab = randLab();
console.log(randomLab);
// Generate a random XYZ color with an array of values scaled to match the nominal 0-1 range
const randomXYZ = randXYZ();
console.log(randomXYZ);
const randomColor = require("visually-random-color");
// Default export is the same as randRGB255
const randomColor = randomColor();
console.log(randomColor);
The returned arrays can be easily used with the inbuilt CSS functions to apply the color to the webpage, along with any other use case you may have.
const Lab = randLab();
const XYZ = randXYZ(...Lab);
const RGB = randRGB255(...XYZ);
document.querySelector(
".Lab"
).style.backgroundColor = `lab(${Lab[0]} ${Lab[1]} ${Lab[2]})`;
document.querySelector(
".XYZ"
).style.backgroundColor = `color(xyz ${XYZ[0]} ${XYZ[1]} ${XYZ[2]})`;
document.querySelector(
".RGB"
).style.backgroundColor = `rgb(${RGB[0]} ${RGB[1]} ${RGB[2]})`;
Due to differences in color spaces, the values in XYZ and RGB may sometimes be out of their nominal range. However, they can still be displayed without issues in CSS. For example, you may get an XYZ value [-0.23, 0.47, 0.12], since you need to leave the nominal color space to reach the color given by Lab.
The package comes with a test suite. Due to the random nature of the output, and sometimes leaving the nominal range (see note above), the test suites are not terribly descriptive. Instead, however, they use a volumetric approach, trying to make sure that no outlying randomness can break it.
You can run the tests yourself in the command line by installing vitest with npm i, and running all tests with npm test
npm i
npm test
Contributions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A simple, lightweight package with no dependencies to generate a random color with as little visual skew as possible
The npm package visually-random-color receives a total of 3 weekly downloads. As such, visually-random-color popularity was classified as not popular.
We found that visually-random-color demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.