This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement color palette fabrucs
- Loading branch information
1 parent
597264a
commit bac5ba1
Showing
2 changed files
with
49 additions
and
55 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
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,40 @@ | ||
import { css } from 'styled-components'; | ||
|
||
/** | ||
* | ||
* @param color : hsl => "255, 85%, 58%""; | ||
*/ | ||
|
||
export const getHsl = (color: string) => { | ||
const colors = color.replace(' ', '').split(','); | ||
const hue = Number.parseInt(colors[0], 10); | ||
const saturation = Number.parseFloat(colors[1]); | ||
const lightness = Number.parseFloat(colors[2]); | ||
return { hue, saturation, lightness }; | ||
}; | ||
|
||
const lightnessFn = (number: number, saturation: number) => saturation + 50 - 0.1 * number; | ||
|
||
const saturationFn = (number: number, saturation: number) => { | ||
/** for black and white palette => saturation doesn't change */ | ||
if (saturation < 30 || number === 500) return saturation; | ||
if (number > 500) return saturation - 30; | ||
return saturation - 10; | ||
}; | ||
|
||
const COLOR_NUMBERS = [100, 200, 300, 400, 500, 600, 700, 800]; | ||
|
||
export const createPalette = (color: string, name: string) => { | ||
const hsl = getHsl(color); | ||
|
||
const palette = COLOR_NUMBERS.map((number) => { | ||
const saturation = saturationFn(number, hsl.saturation); | ||
const lightness = lightnessFn(number, hsl.lightness); | ||
|
||
return `--${name}-${number}: ${hsl.hue}, ${saturation}%, ${lightness}%;`; | ||
}); | ||
|
||
return css` | ||
${palette} | ||
`; | ||
}; |