@chriscodesthings/is-css-hex-color
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

is-css-hex-color
Test workflow status NPM Version License: MIT

Function to determine if a string is a CSS hex color code

Description

Checks if a string is a valid CSS hex color code.

Valid CSS hex colors must:

  • begin with a # character
  • contain 3 hex numbers from 0 to ff
  • may also contain a 4th hex number representing an alpha channel
  • be in long (#123456) or short (#123) form

The following are examples of valid CSS hex colour codes:

#6699ee   // nearly cornflowerblue
#6699ee88 // ... with 53% transparency
#69e8     // ... in shorthand

See...


Install

npm install --save @chriscodesthings/is-css-hex-color

Use

import isCSSHexColor from '@chriscodesthings/is-css-hex-color';

console.log(isCSSHexColor("#123abcff"));
// => true

Syntax

isCSSHexColor(str);

Parameters

  • str: text string to test

Return Value

Returns boolean true if str is a valid CSS hex color code, false otherwise.

Examples

// validate after user input
function setColour(col) {
    if( !isCSSHexColor(col)) {
        alert("Please input a valid CSS hex colour");
        return;
    }

    // set some colours
}

See Also...

Readme

Keywords

Package Sidebar

Install

npm i @chriscodesthings/is-css-hex-color

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

6.86 kB

Total Files

8

Last publish

Collaborators

  • chriscodesthings