#

API Docs for: 3.10.3
Show: Inherited Protected Private Deprecated

# Color Class

Defined in: `color/js/color-base.js:1`
Module: color-base
Parent Module: color

Color provides static methods for color conversion.

    Y.Color.toRGB('f00'); // rgb(255, 0, 0)

    Y.Color.toHex('rgb(255, 255, 0)'); // #ffff00

- Index
- Methods
- Properties

## Item Index

### Methods

- _convertTo
- _getAlpha
- _hexToRgb
- _keywordToHex
- _rgbToHex
- convert
- findType
- fromArray
- toArray
- toHex
- toRGB
- toRGBA

### Properties

- CONVERTS static
- KEYWORDS static
- REGEX_HEX static
- REGEX_HEX3 static
- REGEX_RGB static
- STR_HEX static
- STR_RGB static
- STR_RGBA static
- TYPES static

## Methods

### `_convertTo`

(

- `clr`
- `to`

)
String protected

Defined in `color/js/color-base.js:350`

Available since 3.8.0

Converts the provided color string to the value type provided as `to`

#### Parameters:

- `clr` String
- `to` String

#### Returns:

String:

### `_getAlpha`

(

- `clr`

)
Number protected

Defined in `color/js/color-base.js:314`

Available since 3.8.0

Retrives the alpha channel from the provided string. If no alpha channel is present, `1` will be returned.

#### Parameters:

- `clr` String

#### Returns:

Number:

### `_hexToRgb`

(

- `str`
- `[toArray]`

)
String | Array protected

Defined in `color/js/color-base.js:431`

Available since 3.8.0

Processes the hex string into r, g, b values. Will return values as an array, or as an rgb string.

#### Parameters:

- `str` String
- `[toArray]` Boolean optional

#### Returns:

String | Array:

### `_keywordToHex`

(

- `clr`

)
String protected

Defined in `color/js/color-base.js:334`

Available since 3.8.0

Returns the hex value string if found in the KEYWORDS object

#### Parameters:

- `clr` String

#### Returns:

String:

### `_rgbToHex`

(

- `str`
- `[toArray]`

)
String | Array protected

Defined in `color/js/color-base.js:462`

Available since 3.8.0

Processes the rgb string into r, g, b values. Will return values as an array, or as a hex string.

#### Parameters:

- `str` String
- `[toArray]` Boolean optional

#### Returns:

String | Array:

### `convert`

(

- `str`
- `to`

)
String public

Defined in `color/js/color-base.js:121`

Available since 3.8.0

Converts the provided string to the provided type. You can use the `Y.Color.TYPES` to get a valid `to` type. If the color cannot be converted, the original color will be returned.

#### Parameters:

- `str` String
- `to` String

#### Returns:

String:

### `findType`

(

- `str`

)
String public

Defined in `color/js/color-base.js:286`

Available since 3.8.0

Finds the value type based on the str value provided.

#### Parameters:

- `str` String

#### Returns:

String:

### `fromArray`

(

- `arr`
- `template`

)
String public

Defined in `color/js/color-base.js:255`

Available since 3.8.0

Converts the array of values to a string based on the provided template.

#### Parameters:

- `arr` Array
- `template` String

#### Returns:

String:

### `toArray`

(

- `str`

)
Array public

Defined in `color/js/color-base.js:190`

Available since 3.8.0

Converts the provided color string to an array of values where the last value is the alpha value. Will return an empty array if the provided string is not able to be parsed.

    NOTE: (\ufffe)? is added to HEX and HEX3 Regular Expressions to
    carve out a place for the alpha channel that is returned from
    toArray without compromising any usage of the Regular Expression

    Y.Color.toArray('fff');              // ['ff', 'ff', 'ff', 1]
    Y.Color.toArray('rgb(0, 0, 0)');     // ['0', '0', '0', 1]
    Y.Color.toArray('rgba(0, 0, 0, 0)'); // ['0', '0', '0', 1]

#### Parameters:

- `str` String

#### Returns:

Array:

### `toHex`

(

- `str`

)
String public

Defined in `color/js/color-base.js:144`

Available since 3.8.0

Converts provided color value to a hex value string

#### Parameters:

- `str` String

  Hex or RGB value string

#### Returns:

String: returns array of values or CSS string if options.css is true

### `toRGB`

(

- `str`

)
String public

Defined in `color/js/color-base.js:164`

Available since 3.8.0

Converts provided color value to an RGB value string

#### Parameters:

- `str` String

  Hex or RGB value string

#### Returns:

String:

### `toRGBA`

(

- `str`

)
String public

Defined in `color/js/color-base.js:177`

Available since 3.8.0

Converts provided color value to an RGB value string

#### Parameters:

- `str` String

  Hex or RGB value string

#### Returns:

String:

## Properties

### `CONVERTS`

Object static

Defined in `color/js/color-base.js:112`

Available since 3.8.0

Default: {}

### `KEYWORDS`

Object static

Defined in `color/js/color-base.js:22`

Available since 3.8.0

### `REGEX_HEX`

RegExp static

Defined in `color/js/color-base.js:35`

Available since 3.8.0

NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a place for the alpha channel that is returned from toArray without compromising any usage of the Regular Expression

Default: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})(\ufffe)?/

### `REGEX_HEX3`

RegExp static

Defined in `color/js/color-base.js:48`

Available since 3.8.0

NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a place for the alpha channel that is returned from toArray without compromising any usage of the Regular Expression

Default: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})(\ufffe)?/

### `REGEX_RGB`

RegExp static

Defined in `color/js/color-base.js:61`

Available since 3.8.0

Default: /rgba?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}),? ?([.0-9]{1,3})?\)/

### `STR_HEX`

String static

Defined in `color/js/color-base.js:76`

Available since 3.8.0

Default: #{*}{*}{*}

### `STR_RGB`

String static

Defined in `color/js/color-base.js:85`

Available since 3.8.0

Default: rgb({*}, {*}, {*})

### `STR_RGBA`

String static

Defined in `color/js/color-base.js:94`

Available since 3.8.0

Default: rgba({*}, {*}, {*}, {*})

### `TYPES`

Object static

Defined in `color/js/color-base.js:103`

Available since 3.8.0

Default: {'rgb':'rgb', 'rgba':'rgba'}