Skip to content

Commit

Permalink
Merge pull request #79 from heyjul3s/feat/hextorgba
Browse files Browse the repository at this point in the history
hexToRGBA
  • Loading branch information
heyjul3s committed Feb 11, 2021
2 parents 51457c1 + 4877ac9 commit b90170e
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 27 deletions.
10 changes: 1 addition & 9 deletions packages/hextorgb/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# `@artifak/hextorgb`

A utility function to convert hexadecimal colour values to RGB or RGBA string.

## Usage

```
const hextorgb = require('@artifak/hextorgb');
// TODO: DEMONSTRATE API
```
A utility function to convert hexadecimal colour values to RGB string.
22 changes: 11 additions & 11 deletions packages/hextorgb/__tests__/hextorgb.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
hexToRGBA,
hexToRGB,
getRGBvalues,
splitHexToRGB,
formatHexValue,
expandShorthandHex,
Expand Down Expand Up @@ -68,33 +68,33 @@ describe('@artifak/hextorgb', () => {
});
});

describe('hexToRGB', () => {
describe('getRGBvalues', () => {
it('returns an object with RGB values', () => {
expect(hexToRGB('#DF3EA1')).toMatchObject({
expect(getRGBvalues('#DF3EA1')).toMatchObject({
r: 223,
g: 62,
b: 161
});
});

it('returns UNDEFINED with invalid hex string values', () => {
expect(hexToRGB('Hello World')).toEqual(void 0);
expect(getRGBvalues('Hello World')).toEqual(void 0);
});
});

describe('hexToRGBA', () => {
it('returns a string RGBA value with valid hex and alpha values', () => {
expect(hexToRGBA('#DF3EA1', 0.5)).toEqual('rgba(223, 62, 161, 0.5)');
expect(hexToRGBA('#df3ea1', 0.5)).toEqual('rgba(223, 62, 161, 0.5)');
describe('hexToRGB', () => {
it('returns a string RGB value with valid hex and alpha values', () => {
expect(hexToRGB('#DF3EA1')).toEqual('rgb(223, 62, 161)');
expect(hexToRGB('#df3ea1')).toEqual('rgb(223, 62, 161)');
});

it('returns a string RGBA value with valid hex and default alpha values', () => {
expect(hexToRGBA('#DF3EA1')).toEqual('rgba(223, 62, 161, 1)');
expect(hexToRGBA('#df3ea1')).toEqual('rgba(223, 62, 161, 1)');
expect(hexToRGB('#DF3EA1')).toEqual('rgb(223, 62, 161)');
expect(hexToRGB('#df3ea1')).toEqual('rgb(223, 62, 161)');
});

it('returns UNDEFINED with invalid non-hexadecimal color string value', () => {
expect(hexToRGBA('Lorem Ipsum Dolor Sit Amet', 0.5)).toEqual(void 0);
expect(hexToRGB('Lorem Ipsum Dolor Sit Amet')).toEqual(void 0);
});
});
});
9 changes: 7 additions & 2 deletions packages/hextorgb/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"name": "@artifak/hextorgb",
"version": "1.0.0",
"description": "Utility function to convert style hex colour codes to RGB or RGBA strings.",
"keywords": [],
"description": "Utility function to convert style hex colour codes to RGB string.",
"keywords": [
"color",
"RGB",
"hexadecimal",
"utilities"
],
"author": "Julian Low",
"license": "MIT",
"sideEffects": false,
Expand Down
8 changes: 4 additions & 4 deletions packages/hextorgb/src/hextorgb.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { RGBColor } from './typings';

export function hexToRGBA(hex: string, alpha = 1): string | void {
const color = hexToRGB(hex);
export function hexToRGB(hex: string): string | void {
const color = getRGBvalues(hex);

if (!!color) {
return `rgba(${color.r}, ${color.g}, ${color.b}, ${alpha})`;
return `rgb(${color.r}, ${color.g}, ${color.b})`;
}
}

export function hexToRGB(hex: string): RGBColor | void {
export function getRGBvalues(hex: string): RGBColor | void {
const hexValue = formatHexValue(hex);

if (!!hexValue) {
Expand Down
2 changes: 1 addition & 1 deletion packages/hextorgb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { hexToRGB, hexToRGBA } from './hextorgb';
export { hexToRGB, getRGBvalues } from './hextorgb';
export { RGBColor } from './typings';
3 changes: 3 additions & 0 deletions packages/hextorgba/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@artifak/hextorgba`

A utility function to convert hexadecimal colour values to RGBA string.
17 changes: 17 additions & 0 deletions packages/hextorgba/__tests__/hextorgba.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { hexToRGBA } from '../src/hextorgba';

describe('@artifak/hextorgba', () => {
it('returns a string RGBA value with valid hex and alpha values', () => {
expect(hexToRGBA('#DF3EA1', 0.5)).toEqual('rgba(223, 62, 161, 0.5)');
expect(hexToRGBA('#df3ea1', 0.5)).toEqual('rgba(223, 62, 161, 0.5)');
});

it('returns a string RGBA value with valid hex and default alpha values', () => {
expect(hexToRGBA('#DF3EA1')).toEqual('rgba(223, 62, 161, 1)');
expect(hexToRGBA('#df3ea1')).toEqual('rgba(223, 62, 161, 1)');
});

it('returns UNDEFINED with invalid non-hexadecimal color string value', () => {
expect(hexToRGBA('Lorem Ipsum Dolor Sit Amet', 0.5)).toEqual(void 0);
});
});
46 changes: 46 additions & 0 deletions packages/hextorgba/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@artifak/hextorgba",
"version": "1.0.0",
"description": "Utility function to convert style hex colour codes to RGBA string.",
"keywords": [
"color",
"RGBA",
"hexadecimal",
"utilities"
],
"author": "Julian Low",
"license": "MIT",
"sideEffects": false,
"main": "dist/hextorgba.cjs.js",
"module": "dist/hextorgba.esm.js",
"src": "src/index.ts",
"types": "dist/src/index.d.ts",
"directories": {
"lib": "src",
"test": "__tests__"
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/heyjul3s/artifak.git"
},
"scripts": {
"build": "rimraf dist && builder",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/heyjul3s/artifak/issues"
},
"homepage": "https://github.com/heyjul3s/artifak#readme",
"devDependencies": {
"@artifak/bundler": "^1.1.3"
},
"dependencies": {
"@artifak/hextorgb": "^1.0.0"
}
}
9 changes: 9 additions & 0 deletions packages/hextorgba/src/hextorgba.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getRGBvalues } from '@artifak/hextorgb';

export function hexToRGBA(hex: string, alpha = 1): string | void {
const color = getRGBvalues(hex);

if (!!color) {
return `rgba(${color.r}, ${color.g}, ${color.b}, ${alpha})`;
}
}
2 changes: 2 additions & 0 deletions packages/hextorgba/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { hexToRGBA } from './hextorgba';
export { getRGBvalues } from '@artifak/hextorgb';
9 changes: 9 additions & 0 deletions packages/hextorgba/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "."
},
"references": [],
"include": ["src"]
}

0 comments on commit b90170e

Please sign in to comment.