Created
June 7, 2021 20:04
-
-
Save kyleshevlin/8b44e8391663e6c7ff6159c3d3de445c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { styled } from '@stitches/react' | |
const clamp = (min: number, max: number) => (num: number) => | |
Math.min(Math.max(min, num), max) | |
const optionize = fn => (options = {}) => fn(options) | |
const hslColor = (hue: number, sat: number, lum: number) => ({ | |
brighten = 0, | |
darken = 0, | |
desaturate = 0, | |
saturate = 0, | |
shift = 0, | |
}) => { | |
const resultingHue = clamp(0, 360)(hue + shift) | |
const resultingSat = clamp(0, 100)(sat + saturate - desaturate) | |
const resultingLum = clamp(0, 100)(lum + brighten - darken) | |
return `hsl(${resultingHue} ${resultingSat}% ${resultingLum}%)` | |
} | |
const theme = { | |
colors: { | |
accent: optionize(hslColor(150, 45, 55)), | |
contra: optionize(hslColor(350, 70, 55)), | |
}, | |
} | |
// Then use it in a styled component like this... | |
const StyledButton = styled('button', { | |
appearance: 'none', | |
backgroundColor: theme.colors.accent(), | |
border: 'none', | |
color: 'white', | |
padding: bs(0.5), | |
transition: 'all .2s ease', | |
'&:hover': { | |
backgroundColor: theme.colors.accent({ darken: 3 }), | |
cursor: 'pointer', | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment