1.1.1 • Published 6 months ago

chromagic v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Chromagic

Magic between colors. Conversion between color spaces.

Installation

npm install chromagic

Usage

import { convert } from 'chromagic'

// Convert a color from HEX to RGB
const color = convert('#ff0000', 'RGB')
console.log(color) // rgb(255,0,0)

Available Color Spaces

  • RGB / RGBA
  • HEX

API Reference

convert

Converts a color from one color space to another. It always returns a minified string.

  • Parameters:
    • color (string): The color to convert.
    • to (ColorSpace): The color space to convert to.
  • Returns:
    • string: The converted and minified color.
import { convert } from 'chromagic'

convert('#ff0000', 'RGB') // rgb(255,0,0)
convert('rgba(255,0,0,1)', 'HEX') // #FF0000FF

getRGBA

Gets the RGBA values of any color. The color will be converted to RGBA if it is not already in that format.

  • Parameters:
    • color (string): The color to get the RGBA values from.
  • Returns:
    • object: An object with the RGBA values.
      • r (number): The red value.
      • g (number): The green value.
      • b (number): The blue value.
      • a (number): The alpha value.
import { getRGBA } from 'chromagic'

getRGBA('hsl(0, 100%, 50%)') // { r: 255, g: 0, b: 0, a: 1 }

isRGB

Checks if a string is a valid RGB color.

Rules:

  • Must start with rgb(.
  • Must end with ).
  • Must contain 3 numbers separated by commas.
  • Each number must be between 0 and 255.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in RGB format.
import { isRGB } from 'chromagic'

isRGB('rgb(255, 0, 0)') // true

isRGBA

Checks if a string is a valid RGBA color.

Rules:

  • Must start with rgba(.
  • Must end with ).
  • Must contain 4 numbers separated by commas.
  • First three numbers must be between 0 and 255.
  • The last number must be between 0 and 1 with or without decimals.
    • If the number is 1, it can be written as 1.0 or 1.
    • If it is not 1, it can be written as an integer or a decimal.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in RGBA format.
import { isRGBA } from 'chromagic'

isRGBA('rgba(255, 0, 0, 1)') // true

isHEX

Checks if a string is a valid HEX color.

Rules:

  • Must start with #.
  • Must contain 3 or 6 characters or 4 or 8 characters (if with alpha).
  • Each character must be a hexadecimal number (0-9, a-f) case insensitive.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in HEX format.
import { isHEX } from 'chromagic'

isHEX('#f00') // true

isHSL

Checks if a string is a valid HSL color.

Rules:

  • Must start with hsl(.
  • Must end with ).
  • Must contain 3 numbers separated by commas.
  • First number must be between 0 and 360.
  • Second and third numbers must be between 0 and 100 with percentages.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in HSL format.
import { isHSL } from 'chromagic'

isHSL('hsl(0, 100%, 50%)') // true

isHSLA

Checks if a string is a valid HSLA color.

Rules:

  • Must start with hsla(.
  • Must end with ).
  • Must contain 4 numbers separated by commas.
  • First number must be between 0 and 360.
  • Second and third numbers must be between 0 and 100 with percentages.
  • The last number must be between 0 and 1 with or without decimals.
    • If the number is 1, it can be written as 1.0 or 1.
    • If it is not 1, it can be written as an integer or a decimal.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in HSLA format.
import { isHSLA } from 'chromagic'

isHSLA('hsla(0, 100%, 50%, 1)') // true

isCMYK

Checks if a string is a valid CMYK color.

Rules:

  • Must start with cmyk(.
  • Must end with ).
  • Must contain 4 numbers separated by commas.
  • Each number must be between 0 and 100 with or without percentages.
  • Parameters:
    • color (string): The color to check.
  • Returns:
    • boolean: Whether the color is in CMYK format.
import { isCMYK } from 'chromagic'

isCMYK('cmyk(0, 100%, 100%, 0)') // true

License

MIT License

1.1.1

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago