Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rgb #53

Merged
merged 9 commits into from Dec 17, 2016
Merged

Rgb #53

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/helpers/rgb.js
@@ -0,0 +1,48 @@
// @flow

/**
* Returns a string value for the color. The returned result is the smalles possible hex notation.
*
* @example
* // Styles as object usage
* const styles = {
* background: rgb(255, 205, 100),
* background: rgb({ red: 255, green: 205, blue: 100 }),
* }
*
* // styled-components usage
* const div = styled.div`
* background: ${rgb(255, 205, 100)};
* background: ${rgb({ red: 255, green: 205, blue: 100 })};
* `
*
* // CSS in JS Output
*
* element {
* background: "#ffcd64";
* background: "#ffcd64";
* }
*/

type RgbColor = {
red: number,
green: number,
blue: number,
}

function toHex(value) {
const hex = value.toString(16)
return hex.length === 1 ? `0${hex}` : hex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is hex ever just one character and why do we prefix 0 in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you create the hex of 0 it will be '0'. The issue here at hand is that only of 3 parts of a hex color can be reduced only then it should be. Otherwise two characters e.g. prefixed with a 0 are required.

}

function rgb(value: RgbColor | number, green?: number, blue?: number): string {
if (typeof value === 'number' && typeof green === 'number' && typeof blue === 'number') {
return `#${toHex(value)}${toHex(green)}${toHex(blue)}`
} else if (typeof value === 'object' && green === undefined && blue === undefined) {
return `#${toHex(value.red)}${toHex(value.green)}${toHex(value.blue)}`
}

throw new Error('Passed invalid arguments to rgb, please pass multiple numbers e.g. rgb(255, 205, 100) or an object e.g. rgb({ red: 255, green: 205, blue: 100 }).')
}

export default rgb
11 changes: 11 additions & 0 deletions src/helpers/test/__snapshots__/rgb.test.js.snap
@@ -0,0 +1,11 @@
exports[`rgb should convert a rgb object to a hex color 1`] = `
Object {
"background": "#ffcd64",
}
`;

exports[`rgb should convert multiple numbers to a hex color 1`] = `
Object {
"background": "#ffcd64",
}
`;
17 changes: 17 additions & 0 deletions src/helpers/test/rgb.test.js
@@ -0,0 +1,17 @@
// @flow
import rgb from '../rgb'

describe('rgb', () => {
it('should convert multiple numbers to a hex color', () => {
expect({ background: rgb(255, 205, 100) }).toMatchSnapshot()
})

it('should convert a rgb object to a hex color', () => {
expect({ background: rgb({ red: 255, green: 205, blue: 100 }) }).toMatchSnapshot()
})

it('should throw an error if an object and multiple arguments are passed', () => {
expect(() => ({ background: rgb({ red: 255, green: 1, blue: 1 }, 250, 100) }))
.toThrow('Passed invalid arguments to rgb, please pass multiple numbers e.g. rgb(255, 205, 100) or an object e.g. rgb({ red: 255, green: 205, blue: 100 }).')
})
})