-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[color engine] Implement rgb() function
- Loading branch information
1 parent
deec55b
commit 2d4b3fe
Showing
13 changed files
with
144 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import named, { namedColors } from '../named' | ||
|
||
describe('named', () => { | ||
Object.keys(namedColors).forEach(colorName => { | ||
it(`returns the hex string value for recognized color name: ${colorName}`, () => { | ||
const color = named(colorName) | ||
expect(typeof color).toBe('string') | ||
expect(color?.charAt(0)).toBe('#') | ||
}) | ||
}) | ||
|
||
it('is not case sensitive', () => { | ||
expect(named('royalblue')).toBeDefined() | ||
expect(named('RoyalBlue')).toBeDefined() | ||
}) | ||
|
||
const invalid = [ | ||
'#33CCFF', // hex color value | ||
'rgb(127, 255, 255)', // non-hex color value | ||
' blue ', // untrimmed spaces | ||
'transparent', // special keyword | ||
'rainbow', // invalid color string | ||
] | ||
invalid.forEach(str => { | ||
it(`returns undefined for invalid color name: ${str}`, () => { | ||
expect(named(str)).toBeUndefined() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import rgb from '../rgb' | ||
|
||
describe('rgb', () => { | ||
const rgbaHex = ['#FFAACC33'] | ||
rgbaHex.forEach(color => { | ||
it(`returns an object with RGB values in decimal, alpha as fraction, for: ${color}`, () => { | ||
expect(rgb(color)).toEqual({ r: 255, g: 170, b: 204, a: 0.2 }) | ||
}) | ||
}) | ||
|
||
const rgbHex = ['#FFAACC'] | ||
rgbHex.forEach(color => { | ||
it(`returns default alpha value of 1 for: ${color}`, () => { | ||
expect(rgb(color)).toEqual({ r: 255, g: 170, b: 204, a: 1 }) | ||
}) | ||
}) | ||
|
||
it('expands shorthand hex value', () => { | ||
expect(rgb('#FAC3')).toEqual({ r: 255, g: 170, b: 204, a: 0.2 }) | ||
}) | ||
|
||
it('returns RGB values from CSS color names', () => { | ||
expect(rgb('blue')).toEqual({ r: 0, g: 0, b: 255, a: 1 }) | ||
expect(rgb('yellow')).toEqual({ r: 255, g: 255, b: 0, a: 1 }) | ||
}) | ||
|
||
it('returns null if not a valid color string', () => { | ||
expect(rgb('foo')).toBeNull() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import named from './named' | ||
import { isHexString, matchHexString } from './parsers/hexString' | ||
|
||
export interface RGB { | ||
r: number | ||
g: number | ||
b: number | ||
a?: number | ||
} | ||
|
||
export function rgbFromHexString(colorString: string): RGB | null { | ||
const match = matchHexString(colorString) | ||
if (!match) return null | ||
|
||
const rgbValues = match.map(val => { | ||
// Expand if value is shorthand (single digit) hex | ||
if (val.length === 1) val = `${val}${val}` | ||
// Convert hex to decimal | ||
return parseInt(val, 16) | ||
}) | ||
|
||
// Compute alpha as fraction of 255, defaulting to 1 | ||
const alpha = (rgbValues[3] ?? 255) / 255 | ||
|
||
return { r: rgbValues[0], g: rgbValues[1], b: rgbValues[2], a: alpha } | ||
} | ||
|
||
export default function rgb(colorString: string): RGB | null { | ||
// Get hex value if string is a color name | ||
const hexFromName = named(colorString) | ||
if (hexFromName) colorString = hexFromName | ||
|
||
return rgbFromHexString(colorString) | ||
} |
This file was deleted.
Oops, something went wrong.