Hexadecimal Color

Regular expression to match a hexadecimal color.

Usage

var RE_COLOR_HEXADECIMAL = require( '@stdlib/regexp/color-hexadecimal' );

RE_COLOR_HEXADECIMAL

Regular expression to match a full length hexadecimal color.

var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
// returns true

bool = RE_COLOR_HEXADECIMAL.test( '000' );
// returns false

RE_COLOR_HEXADECIMAL.shorthand

Regular expression to match a shorthand hexadecimal color.

var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
// returns false

bool = RE_COLOR_HEXADECIMAL.test( '000' );
// returns true

RE_COLOR_HEXADECIMAL.either

Regular expression to match either a shorthand or a full length hexadecimal color.

var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
// returns true

bool = RE_COLOR_HEXADECIMAL.test( '000' );
// returns true

Examples

var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var RE_COLOR_HEXADECIMAL = require( '@stdlib/regexp/color-hexadecimal' );

function isHexColor( value, mode ) {
    if ( !isString( value ) ) {
        return false;
    }
    if ( mode === 'shorthand' ) {
        return RE_COLOR_HEXADECIMAL.shorthand.test( value );
    }
    if ( mode === 'either' ) {
        return RE_COLOR_HEXADECIMAL.either.test( value );
    }
    return RE_COLOR_HEXADECIMAL.test( value );
}

var bool = isHexColor( 'ffffff', 'full' );
// returns true

bool = isHexColor( '474747', 'either' );
// returns true

bool = isHexColor( '0A5BBE', 'shorthand' );
// returns false

bool = isHexColor( '000', 'full' );
// returns false

bool = isHexColor( '000', 'either' );
// returns true

bool = isHexColor( 'F7b', 'shorthand' );
// returns true

bool = isHexColor( 'abcd', 'either' );
// returns false

bool = isHexColor( '', 'either' );
// returns false

bool = isHexColor( null, 'either' );
// returns false