1.1.0 • Published 1 year ago

als-color-tools v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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
1.1.0

1 year ago

1.0.0

1 year ago