npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hue-saturation-relative-luminance

v1.0.0

Published

Allows you to control Hue, Saturation, and Relative Luminance as if it where a color space. Inputs are the Hue, Saturation, and Relative Luminance of a color, and output is an array of RGB values ranging from 0 to 1, 0 to 255, and a string containing the

Downloads

10

Readme

hue-saturation-relative-luminance

hue-saturation-relative-luminance is a package that allows you to control Hue, Saturation, and Relative Luminance as if it where a color space.

This is different than Hue Saturation Lightness (HSL). Rather, it conforms to W3's definition of Relative Luminance in WCAG22.

Installation

Using npm:

npm install hue-saturation-relative-luminance

Then, in your Node.js code:

import hueSatLum from 'hue-saturation-relative-luminance';

How to Use This Module

import hueSatLum from 'hue-saturation-relative-luminance';

const desiredHue = 180;
const desiredSaturation = 0.8;
const desiredRelativeLuminance = 0.7;

const result = hueSatLum(desiredHue, desiredSaturation, desiredRelativeLuminance);

console.log(result.hex) // '#65eeee'
console.log(result.sRGB) // [0.3953125, 0.9328125, 0.9328125]
console.log(result.RGB255) // [101, 238, 238]

Type Definitions:

function hueSaturationRelativeLuminance(
	inputHue: number, //0-360 (Modulated)
	inputSaturation: number, //0-1 (Clamped)
	inputRelativeLuminance: number //0-1 (Clamped)
): hueSaturationRelativeLuminanceResult

type hueSaturationRelativeLuminanceResult = {
	hex: string, //This is a string containing the hex code of the result.
	RGB255: [number, number, number] //All of these are whole numbers ranging between 0 and 255.
	sRGB: [number, number, number] //All of these are decimal numbers between 0 and 1.
}

Using the Module's Function

Simply use the imported function with the parameters inputHue, inputSaturation, and inputRelativeLuminance to get an object containing RGB values that closely fit the inputs.

inputHue is modulated into a range of 0-360 degrees, just like you'd expect color hue to behave.

inputSaturation and inputRelativeLuminance are both clamped within 0 and 1.

Using the Function's Returned Object

The function returns an object containing the properties .hex, .RGB255, and .sRGB.

.hex is the resulting RGB values as a string, encoded in hexadecimal. It always begins with a #, followed by six hexadecimal characters. This is most useful in CSS, or anywhere hexadecimal strings are accepted.

.RGB255 is the resulting RGB values, all ranging from 0 to 255, rounded to the nearest integer. This is often the format needed for displays to show your color, and is great for both CSS and image manipulation.

.sRGB is the resulting RGB values, all between 0 and 1. This is most most percise output, and is great if you need to do additional math or convert the result to a different format.

Extra Exported Functions

There are 2 functions that are used internally that are exported for convenience.

getRelativeLuminance(inputSRGB: [number, number, number]): number

import { getRelativeLuminance } from 'hue-saturation-relative-luminance';

console.log(getRelativeLuminance([0.3953125, 0.9328125, 0.9328125])) //0.6999244338098827

This function takes in an array of three sRGB values (between 0 and 1), then returns the relative luminance of those RGB values. No values are clamped in this process.

HSLtoSRGB(inputHue: number, inputSaturation: number, inputLightness: number): SRGBColor

import { HSLtoSRGB } from 'hue-saturation-relative-luminance';

const desiredHue = 180;
const desiredSaturation = 0.8;
const desiredLightness = 0.7;

console.log(HSLtoSRGB(desiredHue, desiredSaturation, desiredLightness)); //[ 0.4599999999999999, 0.94, 0.94 ]

This function takes in a Hue, Saturation, and a Lightness value and returns an array of three sRGB values. The input hue is modulated into a range of 0-360 degrees, just like you'd expect color hue to behave.

Examples & Usecases

Guaranteeing Contrast on Text With Custom Colors

import HueSatLum from 'hue-saturation-relative-luminance';

let originalHue = 240; //Assumed to be the hue input by the user. 
let originalSaturation = 1.0; //Assumed to be the saturation input by the user. 

//In our web app, the background of the page is pitch black, so a pure blue will be too hard to read.
//Let's change that.

let result = HueSatLum(originalHue, originalSaturation, 0.5)

console.log(result.hex) // '#b5b5ff'
//This is perfect to insert into the style data of our web app.

Guaranteeing Contrast on Text With Custom Colors without Unnessassary Changes.

import HueSatLum from 'hue-saturation-relative-luminance';
import { getRelativeLuminance, HSLtoSRGB } from 'hue-saturation-relative-luminance';

let originalHue = 180; //Assumed to be the hue input by the user. 
let originalSaturation = 1.0; //Assumed to be the saturation input by the user. 

//This time, an aqua color is quite easy to read in our web app's black background. So we don't want to change it.
//Instead, we'll check if it passes a minimum relative luminance level.

function increaseContrastIfLow(inputHue, inputSaturation) {

    let originalColor = HSLtoSRGB(inputHue, inputSaturation, 0.5);

    if (getRelativeLuminance(originalColor) > 0.5) {

        return HueSatLum(inputHue, inputSaturation, getRelativeLuminance(originalColor))

    } else {

        return HueSatLum(inputHue, inputSaturation, 0.5);

    }

}

let result = increaseContrastIfLow(originalHue, originalSaturation)

console.log(result.hex) // '#00ffff'
//This is perfect to insert into the style data of our web app.

Issues

If you find any bugs, problems, or ways to improve this package, consider creating an issue on GitHub. I'll be happy to help!