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
Rgb #53
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7322c66
WIP
nikgraf 7ec897a
chore(rgb): add first draft of rgb
nikgraf a46a8f1
Merge branch 'master' into rgb
nikgraf a5d3080
Merge branch 'master' into rgb
nikgraf 607ddca
chore(internal): add internal reduceHexValue helper
nikgraf 11e159a
chore(rgb): reduce hex values in rgb
nikgraf ee7dafe
chore(numberToHex): move numberToHex to internal
nikgraf d884f54
chore(rgb): export rgb and add color to the documentation
nikgraf b69fc1d
chore(rgb): move comment to fix docs
nikgraf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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 | ||
} | ||
|
||
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 |
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,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", | ||
} | ||
`; |
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,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 }).') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 a0
are required.