├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── package.json ├── license ├── index.js ├── readme.md ├── index.d.ts └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/hex-rgb 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import hexRgb, {RgbaObject, RgbaTuple} from './index.js'; 3 | 4 | expectType(hexRgb('#cd2222cc')); 5 | expectType(hexRgb('#cd2222cc', {format: 'array'})); 6 | expectType(hexRgb('#cd2222cc', {format: 'array', alpha: 0.5})); 7 | expectType(hexRgb('#cd2222cc', {format: 'css'})); 8 | expectType(hexRgb('#cd2222cc', {format: 'object'})); 9 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hex-rgb", 3 | "version": "5.0.0", 4 | "description": "Convert HEX color to RGBA", 5 | "license": "MIT", 6 | "repository": "sindresorhus/hex-rgb", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "hex", 27 | "rgb", 28 | "rgba", 29 | "color", 30 | "colour", 31 | "convert", 32 | "conversion", 33 | "converter", 34 | "css" 35 | ], 36 | "devDependencies": { 37 | "ava": "^3.15.0", 38 | "tsd": "^0.14.0", 39 | "xo": "^0.39.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const hexCharacters = 'a-f\\d'; 2 | const match3or4Hex = `#?[${hexCharacters}]{3}[${hexCharacters}]?`; 3 | const match6or8Hex = `#?[${hexCharacters}]{6}([${hexCharacters}]{2})?`; 4 | const nonHexChars = new RegExp(`[^#${hexCharacters}]`, 'gi'); 5 | const validHexSize = new RegExp(`^${match3or4Hex}$|^${match6or8Hex}$`, 'i'); 6 | 7 | export default function hexRgb(hex, options = {}) { 8 | if (typeof hex !== 'string' || nonHexChars.test(hex) || !validHexSize.test(hex)) { 9 | throw new TypeError('Expected a valid hex string'); 10 | } 11 | 12 | hex = hex.replace(/^#/, ''); 13 | let alphaFromHex = 1; 14 | 15 | if (hex.length === 8) { 16 | alphaFromHex = Number.parseInt(hex.slice(6, 8), 16) / 255; 17 | hex = hex.slice(0, 6); 18 | } 19 | 20 | if (hex.length === 4) { 21 | alphaFromHex = Number.parseInt(hex.slice(3, 4).repeat(2), 16) / 255; 22 | hex = hex.slice(0, 3); 23 | } 24 | 25 | if (hex.length === 3) { 26 | hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; 27 | } 28 | 29 | const number = Number.parseInt(hex, 16); 30 | const red = number >> 16; 31 | const green = (number >> 8) & 255; 32 | const blue = number & 255; 33 | const alpha = typeof options.alpha === 'number' ? options.alpha : alphaFromHex; 34 | 35 | if (options.format === 'array') { 36 | return [red, green, blue, alpha]; 37 | } 38 | 39 | if (options.format === 'css') { 40 | const alphaString = alpha === 1 ? '' : ` / ${Number((alpha * 100).toFixed(2))}%`; 41 | return `rgb(${red} ${green} ${blue}${alphaString})`; 42 | } 43 | 44 | return {red, green, blue, alpha}; 45 | } 46 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # hex-rgb 2 | 3 | > Convert HEX color to RGBA 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install hex-rgb 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import hexRgb from 'hex-rgb'; 15 | 16 | hexRgb('4183c4'); 17 | //=> {red: 65, green: 131, blue: 196, alpha: 1} 18 | 19 | hexRgb('#4183c4'); 20 | //=> {red: 65, green: 131, blue: 196, alpha: 1} 21 | 22 | hexRgb('#fff'); 23 | //=> {red: 255, green: 255, blue: 255, alpha: 1} 24 | 25 | hexRgb('#22222299'); 26 | //=> {red: 34, green: 34, blue: 34, alpha: 0.6} 27 | 28 | hexRgb('#0006'); 29 | //=> {red: 0, green: 0, blue: 0, alpha: 0.4} 30 | 31 | hexRgb('#cd2222cc'); 32 | //=> {red: 205, green: 34, blue: 34, alpha: 0.8} 33 | 34 | hexRgb('#cd2222cc', {format: 'array'}); 35 | //=> [205, 34, 34, 0.8] 36 | 37 | hexRgb('#cd2222cc', {format: 'css'}); 38 | //=> 'rgb(205 34 34 / 80%)' 39 | 40 | hexRgb('#000', {format: 'css'}); 41 | //=> 'rgb(0 0 0)' 42 | 43 | hexRgb('#22222299', {alpha: 1}); 44 | //=> {red: 34, green: 34, blue: 34, alpha: 1} 45 | 46 | hexRgb('#fff', {alpha: 0.5}); 47 | //=> {red: 255, green: 255, blue: 255, alpha: 0.5} 48 | ``` 49 | 50 | ## API 51 | 52 | ### hexRgb(hex, options?) 53 | 54 | #### hex 55 | 56 | Type: `string` 57 | 58 | The color in HEX format. Leading `#` is optional. 59 | 60 | #### options 61 | 62 | Type: `object` 63 | 64 | ##### format 65 | 66 | Type: `string`\ 67 | Values: `'object' | 'array' | 'css'`\ 68 | Defaults: `'object'` 69 | 70 | The RGB output format. 71 | 72 | Note that when using the `css` format, the value of the alpha channel is rounded to two decimal places. 73 | 74 | ##### alpha 75 | 76 | Type: `number` 77 | 78 | Set the alpha of the color. 79 | 80 | This overrides any existing alpha component in the Hex color string. For example, the `99` in `#22222299`. 81 | 82 | The number must be in the range 0 to 1. 83 | 84 | ## Related 85 | 86 | See [rgb-hex](https://github.com/sindresorhus/rgb-hex) for the inverse. 87 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-redeclare */ 2 | 3 | export interface Options { 4 | /** 5 | The RGB output format. 6 | 7 | Note that when using the `css` format, the value of the alpha channel is rounded to two decimal places. 8 | 9 | @default 'object' 10 | */ 11 | readonly format?: 'object' | 'array' | 'css'; 12 | 13 | /** 14 | Set the alpha of the color. 15 | 16 | This overrides any existing alpha component in the Hex color string. For example, the `99` in `#22222299`. 17 | 18 | The number must be in the range 0 to 1. 19 | */ 20 | readonly alpha?: number; 21 | } 22 | 23 | export interface RgbaObject { 24 | red: number; 25 | green: number; 26 | blue: number; 27 | alpha: number; 28 | } 29 | 30 | export type RgbaTuple = [ 31 | red: number, 32 | green: number, 33 | blue: number, 34 | alpha: number 35 | ]; 36 | 37 | /** 38 | Convert HEX color to RGBA. 39 | 40 | @param hex - The color in HEX format. Leading `#` is optional. 41 | 42 | @example 43 | ``` 44 | import hexRgb from 'hex-rgb'; 45 | 46 | hexRgb('4183c4'); 47 | //=> {red: 65, green: 131, blue: 196, alpha: 1} 48 | 49 | hexRgb('#4183c4'); 50 | //=> {red: 65, green: 131, blue: 196, alpha: 1} 51 | 52 | hexRgb('#fff'); 53 | //=> {red: 255, green: 255, blue: 255, alpha: 1} 54 | 55 | hexRgb('#22222299'); 56 | //=> {red: 34, green: 34, blue: 34, alpha: 0.6} 57 | 58 | hexRgb('#0006'); 59 | //=> {red: 0, green: 0, blue: 0, alpha: 0.4} 60 | 61 | hexRgb('#cd2222cc'); 62 | //=> {red: 205, green: 34, blue: 34, alpha: 0.8} 63 | 64 | hexRgb('#cd2222cc', {format: 'array'}); 65 | //=> [205, 34, 34, 0.8] 66 | 67 | hexRgb('#cd2222cc', {format: 'css'}); 68 | //=> 'rgb(205 34 34 / 80%)' 69 | 70 | hexRgb('#000', {format: 'css'}); 71 | //=> 'rgb(0 0 0)' 72 | 73 | hexRgb('#22222299', {alpha: 1}); 74 | //=> {red: 34, green: 34, blue: 34, alpha: 1} 75 | 76 | hexRgb('#fff', {alpha: 0.5}); 77 | //=> {red: 255, green: 255, blue: 255, alpha: 0.5} 78 | ``` 79 | */ 80 | export default function hexRgb(hex: string): RgbaObject; 81 | export default function hexRgb(hex: string, options: Options & {format: 'object'}): RgbaObject; // eslint-disable-line @typescript-eslint/unified-signatures 82 | export default function hexRgb(hex: string, options: Options & {format: 'array'}): RgbaTuple; 83 | export default function hexRgb(hex: string, options: Options & {format: 'css'}): string; 84 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import hexRgb from './index.js'; 3 | 4 | const reject = (t, string) => { 5 | return t.throws(() => { 6 | hexRgb(string); 7 | }, { 8 | instanceOf: TypeError 9 | }).message; 10 | }; 11 | 12 | test('rejects', t => { 13 | const message = 'Expected a valid hex string'; 14 | t.is(reject(t, 123), message); 15 | t.is(reject(t, '🦄'), message); 16 | t.is(reject(t, '#12🦄'), message); 17 | t.is(reject(t, '#12345'), message); 18 | t.is(reject(t, '#1234567'), message); 19 | t.is(reject(t, '#123456789'), message); 20 | }); 21 | 22 | test('hex; output object', t => { 23 | const options = {format: 'object'}; 24 | t.deepEqual(hexRgb('#4183c4', options), {red: 65, green: 131, blue: 196, alpha: 1}); 25 | t.deepEqual(hexRgb('#000', options), {red: 0, green: 0, blue: 0, alpha: 1}); 26 | t.deepEqual(hexRgb('#0006', options), {red: 0, green: 0, blue: 0, alpha: 0.4}); 27 | t.deepEqual(hexRgb('4183c488', options), {red: 65, green: 131, blue: 196, alpha: 0.5333333333333333}); 28 | t.deepEqual(hexRgb('#4183c488'), {red: 65, green: 131, blue: 196, alpha: 0.5333333333333333}); 29 | t.deepEqual(hexRgb('#4183c488', {alpha: 0.8}), {red: 65, green: 131, blue: 196, alpha: 0.8}); 30 | t.deepEqual(hexRgb('4183c4'), {red: 65, green: 131, blue: 196, alpha: 1}); 31 | t.deepEqual(hexRgb('#000f'), {red: 0, green: 0, blue: 0, alpha: 1}); 32 | t.deepEqual(hexRgb('#000', {alpha: 0.5}), {red: 0, green: 0, blue: 0, alpha: 0.5}); 33 | t.deepEqual(hexRgb('#22222299'), {red: 34, green: 34, blue: 34, alpha: 0.6}); 34 | t.deepEqual(hexRgb('#cd2222cc'), {red: 205, green: 34, blue: 34, alpha: 0.8}); 35 | }); 36 | 37 | test('hex; output array', t => { 38 | const options = {format: 'array'}; 39 | 40 | t.deepEqual(hexRgb('4183c4', options), [65, 131, 196, 1]); 41 | t.deepEqual(hexRgb('#4183c4', options), [65, 131, 196, 1]); 42 | t.deepEqual(hexRgb('#4183c4', {...options, alpha: 0.02}), [65, 131, 196, 0.02]); 43 | t.deepEqual(hexRgb('#000', options), [0, 0, 0, 1]); 44 | t.deepEqual(hexRgb('#000', {...options, alpha: 0.1}), [0, 0, 0, 0.1]); 45 | t.deepEqual(hexRgb('4183c488', options), [65, 131, 196, 0.5333333333333333]); 46 | t.deepEqual(hexRgb('#4183c488', options), [65, 131, 196, 0.5333333333333333]); 47 | t.deepEqual(hexRgb('#4183c488', {...options, alpha: 1}), [65, 131, 196, 1]); 48 | t.deepEqual(hexRgb('#0008', options), [0, 0, 0, 0.5333333333333333]); 49 | t.deepEqual(hexRgb('#000f', options), [0, 0, 0, 1]); 50 | }); 51 | 52 | test('hex; output css', t => { 53 | const options = {format: 'css'}; 54 | t.is(hexRgb('4183c4', options), 'rgb(65 131 196)'); 55 | t.is(hexRgb('#4183c4', options), 'rgb(65 131 196)'); 56 | t.is(hexRgb('#4183c4', {...options, alpha: 0.5}), 'rgb(65 131 196 / 50%)'); 57 | t.is(hexRgb('#000', options), 'rgb(0 0 0)'); 58 | t.is(hexRgb('4183c488', options), 'rgb(65 131 196 / 53.33%)'); 59 | t.is(hexRgb('#4183c488', options), 'rgb(65 131 196 / 53.33%)'); 60 | t.is(hexRgb('4183c488', {...options, alpha: 1}), 'rgb(65 131 196)'); 61 | t.is(hexRgb('#0008', options), 'rgb(0 0 0 / 53.33%)'); 62 | t.is(hexRgb('#000f', options), 'rgb(0 0 0)'); 63 | }); 64 | --------------------------------------------------------------------------------