Skip to content

Commit

Permalink
Merge pull request #2 from arnelenero/feature/color-engine
Browse files Browse the repository at this point in the history
Feature/color engine
  • Loading branch information
arnelenero committed Apr 1, 2022
2 parents 6973a56 + b5de3bc commit 0f80cf9
Show file tree
Hide file tree
Showing 34 changed files with 1,081 additions and 158 deletions.
111 changes: 7 additions & 104 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simpler-color",
"version": "0.1.3",
"version": "0.2.0",
"description": "Simpler Color - Create your own complete color system fast and easy!",
"keywords": [
"javascript",
Expand Down Expand Up @@ -34,8 +34,7 @@
"test:cov-local": "npm test -- --coverage"
},
"dependencies": {
"@babel/runtime": "^7.17.8",
"color": "^4.2.0"
"@babel/runtime": "^7.17.8"
},
"devDependencies": {
"@babel/cli": "^7.17.6",
Expand Down
7 changes: 0 additions & 7 deletions src/__tests__/normalize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Color from 'color'

import normalize from '../normalize'

describe('normalize', () => {
Expand All @@ -23,11 +21,6 @@ describe('normalize', () => {
})
})

it('supports `Color` object as input', () => {
const color = Color('blue')
expect(normalize(color)).toBe('#0000FF')
})

it('throws if color value is invalid', () => {
expect(() => {
return normalize('bluish')
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { clamp } from '../utils'

describe('clamp', () => {
it('returns the same value if it is within range', () => {
expect(clamp(50, 0, 100)).toBe(50)
})

it('limits the value to the minimum', () => {
expect(clamp(-1, 0, 100)).toBe(0)
})

it('limits the value to the maximum', () => {
expect(clamp(101, 0, 100)).toBe(100)
})
})
28 changes: 28 additions & 0 deletions src/color/__tests__/angle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import angle from '../angle'

describe('angle', () => {
let values: string[]

values = ['180deg', '180']
values.forEach(val => {
it(`extracts straight value if angle string is in degrees (default): ${val}`, () => {
expect(angle(val)).toBe(180)
})
})

values = ['3.14159265rad', '200grad', '0.5turn']
values.forEach(val => {
it(`converts value to degrees if angle string is in a different unit: ${val}`, () => {
expect(Math.round(angle(val))).toBe(180)
})
})

it('normalizes value to range [0..360) degrees', () => {
expect(angle('-90deg')).toBe(270)
expect(angle('360deg')).toBe(0)
})

it('returns NaN if argument is not a valid numeric value', () => {
expect(angle('invalid')).toBeNaN()
})
})
28 changes: 28 additions & 0 deletions src/color/__tests__/colorString.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import colorString from '../colorString'

describe('colorString', () => {
it('returns a hex string from RGB object when alpha = 1', () => {
const obj = { r: 255, g: 0, b: 255, a: 1 }
expect(colorString(obj)).toBe('#FF00FF')
})

it('returns a hex string from HSL object when alpha = 1', () => {
const obj = { h: 240, s: 100, l: 50, a: 1 }
expect(colorString(obj)).toBe('#0000FF')
})

it('returns an rgba() string from RGB object when alpha < 1', () => {
const obj = { r: 255, g: 0, b: 255, a: 0.6 }
expect(colorString(obj)).toBe('rgba(255, 0, 255, 0.6)')
})

it('returns an rgba() string from HSL object when alpha < 1', () => {
const obj = { h: 240, s: 100, l: 50, a: 0.8 }
expect(colorString(obj)).toBe('rgba(0, 0, 255, 0.8)')
})

it('rounds off r,g,b values in rgba() strings', () => {
const obj = { r: 127.5, g: 64.3333, b: 255, a: 0.6 }
expect(colorString(obj)).toBe('rgba(128, 64, 255, 0.6)')
})
})
Loading

0 comments on commit 0f80cf9

Please sign in to comment.