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

als-color-tools

v1.1.0

Published

The `ColorTools` class provides various static methods to handle color transformations and calculations. It supports conversions between HEX, RGB, and HSL color formats, and includes additional utility methods.

Downloads

19

Readme

ColorTools

The ColorTools class provides various static methods to handle color transformations and calculations. It supports conversions between HEX, RGB, and HSL color formats, and includes additional utility methods.

Summary for tools:

const ColorTools = require('als-color-tools')
// or <script src="node_modules/als-color-tools/color-tools.js"></script>
const {hexToRGB,rgbToHex,hexToHSL,hslToHex,shade,luma,isDark} = ColorTools

hexToRGB(hex)
rgbToHex(r, g, b)
hexToHSL(H)
hslToHex(h, s, l)
shade(hex, percent, opacity)
luma(hex)
isDark(c, darkThreshold = 100)

static hexToRGB(hex)

Converts a HEX color value to its RGB representation.

  • Parameters
    • hex (String): The HEX color value (e.g., #ff8000 or #f06).
  • Returns
    • An object containing r, g, and b values.

Example:

ColorTools.hexToRGB('#f06') // { r: 255, g: 0, b: 102 }

static rgbToHex(r, g, b)

Converts RGB color components to a HEX color value.

  • Parameters
    • r (Number): Red component (0-255).
    • g (Number): Green component (0-255).
    • b (Number): Blue component (0-255).
  • Returns
    • A HEX color value as a string (e.g., #ff8000).

Example:

rgbToHex(255, 128, 0) // '#ff8000'

static hexToHSL(hex)

Converts a HEX color value to its HSL representation.

  • Parameters
    • hex (String): The HEX color value (e.g., #00ff80 or #f06).
  • Returns
    • An object containing h, s, and l values.

Examle:

hexToHSL('#f06') // { h: 336, s: 100, l: 50 }

static hslToHex(h, s, l)

Converts HSL color components to a HEX color value.

  • Parameters
    • h (Number): Hue component (0-360).
    • s (Number): Saturation component (0-100).
    • l (Number): Lightness component (0-100).
  • Returns
    • A HEX color value as a string (e.g., #ff0066).

Example:

hslToHex(336,100,50) // '#ff0066'

static shade(hex, percent, [opacity])

Shades a HEX color by a certain percentage and optionally sets its opacity.

  • Parameters
    • hex (String): The HEX color value to be shaded.
    • percent (Number): The percentage to shade the color.
    • opacity (Number): Optional opacity value (0-1).
  • Returns
    • A HEX color value as a string or an RGBA color value if opacity is provided.
shade('#777',30,0.5) // rgba(154,154,154,0.5) 30% lighter and opacity 50%
shade('#777',30) // #9a9a9a 30% lighter
shade('#777',-30) // #535353 30% darker

static luma(hex)

Calculates the luma (brightness) of a HEX color value.

  • Parameters
    • hex (String): The HEX color value.
  • Returns
    • A number representing the luma value.

Example:

ColorTools.luma('#000') // 0
ColorTools.luma('#FFF') // 255

static isDark(hex, [darkThreshold])

Determines if a HEX color value is dark based on a threshold.

  • Parameters
    • hex (String): The HEX color value to be checked.
    • darkThreshold (Number): Optional threshold value (default is 100).
  • Returns
    • true if the color is dark, otherwise false.

Example:

ColorTools.isDark('#000') // true
ColorTools.isDark('#FFF') // false

cssColors

This property contains a mapping of CSS color names to their corresponding HEX codes. It is useful for quickly accessing standard CSS color values and for ensuring consistency with named CSS colors in web design.

  • Property Type
    • Object: Each key is a CSS color name (as a String), and each value is the corresponding HEX color code (as a String).

Example:

console.log(ColorTools.cssColors['red']); // Outputs: '#ff0000'
console.log(ColorTools.cssColors['navy']); // Outputs: '#000080'

Usage

You can use cssColors to quickly check or retrieve HEX codes for standard CSS colors:

if ('rebeccapurple' in ColorTools.cssColors) {
  console.log('Color found:', ColorTools.cssColors['rebeccapurple']);
} else {
  console.log('Color not found');
}

This object is particularly helpful when you need to integrate web colors directly in JavaScript without hardcoding values, ensuring that color definitions remain up-to-date and standardized.

static colorToHex(input, [defaultHex])

Converts a named CSS color or a HEX color code to a standard HEX color code. If the input is not a recognized color name or a valid HEX code, it returns a default HEX value.

  • Parameters
    • input (String): The color name or HEX code to convert.
    • defaultHex (String): Optional default HEX color value (default is #000000).
  • Returns
    • A standard HEX color value as a string.

Examples:

ColorTools.colorToHex('red') // '#ff0000'
ColorTools.colorToHex('blue') // '#0000ff'
ColorTools.colorToHex('#0f0') // '#00ff00' converts shorthand HEX code to full
ColorTools.colorToHex('unknowncolor', '#ffffff') // '#ffffff' uses default HEX when the color is unknown
ColorTools.colorToHex('#00ff00') // '#00ff00' returns the same HEX code if already in full format