2018-12-06 21:28:36 +03:00
|
|
|
import React from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import chroma from 'chroma-js'
|
2018-12-06 03:31:36 +03:00
|
|
|
import colors from 'primer-colors'
|
2018-12-06 21:51:35 +03:00
|
|
|
import titleCase from 'title-case'
|
2018-12-06 03:31:36 +03:00
|
|
|
import {BorderBox, Box, Flex, Heading, Text} from '@primer/components'
|
|
|
|
|
|
|
|
const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red']
|
|
|
|
|
2018-12-06 22:48:33 +03:00
|
|
|
const {black: BLACK, white: WHITE} = colors
|
2018-12-06 21:28:36 +03:00
|
|
|
|
2018-12-06 03:31:36 +03:00
|
|
|
export function ColorPalette(props) {
|
|
|
|
return (
|
2018-12-06 21:51:35 +03:00
|
|
|
<Flex mb={6} className="markdown-no-margin" {...props}>
|
|
|
|
{gradientHues.map(hue => {
|
|
|
|
const color = colors[hue][5]
|
|
|
|
return (
|
|
|
|
<Box bg={color} p={3} width={200} mr={2} key={hue}>
|
2018-12-06 22:48:33 +03:00
|
|
|
<Text fontWeight="bold" color={overlayColor(color)}>
|
2018-12-06 21:51:35 +03:00
|
|
|
{titleCase(hue)}
|
|
|
|
</Text>
|
|
|
|
</Box>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
<BorderBox bg="white" p={3} width={200} borderRadius={0}>
|
|
|
|
<Text fontWeight="bold" color="black">
|
|
|
|
White
|
|
|
|
</Text>
|
2018-12-06 03:31:36 +03:00
|
|
|
</BorderBox>
|
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ColorVariables(props) {
|
|
|
|
return (
|
|
|
|
<>
|
2018-12-06 21:51:35 +03:00
|
|
|
<Flex flexWrap="wrap" className="gutter" {...props}>
|
2018-12-06 03:31:36 +03:00
|
|
|
{gradientHues.map(hue => (
|
2018-12-06 22:48:33 +03:00
|
|
|
<ColorVariable id={hue} hue={hue} key={hue} />
|
2018-12-06 03:31:36 +03:00
|
|
|
))}
|
|
|
|
</Flex>
|
|
|
|
<Flex flexWrap="wrap" {...props}>
|
2018-12-06 22:48:33 +03:00
|
|
|
<FadeVariables id="black" hue="black" bg="black" color="white">
|
2018-12-06 23:10:41 +03:00
|
|
|
<BorderBox border={0} borderRadius={0} borderTop={1} borderColor="gray.5" mt={1}>
|
2019-01-26 01:38:34 +03:00
|
|
|
<Text is="div" fontSize={2} pt={3} mb={0}>
|
2018-12-06 03:31:36 +03:00
|
|
|
Black fades apply alpha transparency to the <Var>$black</Var> variable. The black color value has a slight
|
|
|
|
blue hue to match our grays.
|
|
|
|
</Text>
|
|
|
|
</BorderBox>
|
|
|
|
</FadeVariables>
|
2018-12-06 22:48:33 +03:00
|
|
|
<FadeVariables id="white" hue="white" over={BLACK}>
|
2018-12-06 23:10:41 +03:00
|
|
|
<BorderBox border={0} borderRadius={0} borderTop={1} mt={1}>
|
2019-01-26 01:38:34 +03:00
|
|
|
<Text is="div" fontSize={2} pt={3} mb={0}>
|
2018-12-06 03:31:36 +03:00
|
|
|
White fades apply alpha transparency to the <Var>$white</Var> variable, below these are shown overlaid on
|
|
|
|
a dark gray background.
|
|
|
|
</Text>
|
|
|
|
</BorderBox>
|
|
|
|
</FadeVariables>
|
|
|
|
</Flex>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ColorVariable({hue, ...rest}) {
|
|
|
|
const values = colors[hue]
|
|
|
|
return (
|
2019-01-26 01:38:34 +03:00
|
|
|
<Flex.Item is={Box} pr={4} mb={6} className="col-6 markdown-no-margin" {...rest}>
|
|
|
|
{/* <Heading is="div">{titleCase(hue)}</Heading> */}
|
2018-12-06 03:31:36 +03:00
|
|
|
<Box bg={`${hue}.5`} my={2} p={3} color="white">
|
2019-01-26 01:38:34 +03:00
|
|
|
<Heading is="div" pb={3} fontSize={56} fontWeight="light">
|
2018-12-06 21:51:35 +03:00
|
|
|
{titleCase(hue)}
|
2018-12-06 03:31:36 +03:00
|
|
|
</Heading>
|
|
|
|
<Flex justifyContent="space-between">
|
2019-01-26 01:38:34 +03:00
|
|
|
<Flex.Item flex="1 1 auto" is={Var}>
|
2018-12-06 03:31:36 +03:00
|
|
|
${hue}-500
|
|
|
|
</Flex.Item>
|
|
|
|
<Text fontFamily="mono">{values[5]}</Text>
|
|
|
|
</Flex>
|
|
|
|
</Box>
|
|
|
|
{values.map((value, i) => (
|
|
|
|
<Swatch name={`${hue}-${i}00`} value={value} key={value} />
|
|
|
|
))}
|
|
|
|
</Flex.Item>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:28:36 +03:00
|
|
|
ColorVariable.propTypes = {
|
|
|
|
hue: PropTypes.oneOf(Object.keys(colors)).isRequired
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:31:36 +03:00
|
|
|
export function FadeVariables({hue, color, bg, over, children, ...rest}) {
|
|
|
|
const colorValue = colors[hue]
|
|
|
|
const alphas = [15, 30, 50, 70, 85]
|
2018-12-06 21:28:36 +03:00
|
|
|
const values = alphas.map(alpha => {
|
2018-12-08 01:36:38 +03:00
|
|
|
const value = chroma(colorValue)
|
|
|
|
.alpha(alpha / 100)
|
|
|
|
.css()
|
2018-12-06 21:28:36 +03:00
|
|
|
return {
|
|
|
|
name: `${hue}-fade-${alpha}`,
|
2018-12-06 22:48:33 +03:00
|
|
|
textColor: fadeTextColor(value, over),
|
|
|
|
value
|
2018-12-06 21:28:36 +03:00
|
|
|
}
|
|
|
|
})
|
2018-12-06 03:31:36 +03:00
|
|
|
const boxProps = {color, bg}
|
|
|
|
return (
|
2019-01-26 01:38:34 +03:00
|
|
|
<Flex.Item is={Box} pr={4} mb={6} width={1 / 2} className="markdown-no-margin" {...rest}>
|
|
|
|
{/* <Heading is="div">{titleCase(hue)}</Heading> */}
|
2018-12-06 03:31:36 +03:00
|
|
|
<Box my={2} p={3} {...boxProps}>
|
2019-01-26 01:38:34 +03:00
|
|
|
<Heading is="div" pb={3} fontSize={56} fontWeight="light">
|
2018-12-06 21:51:35 +03:00
|
|
|
{titleCase(hue)}
|
2018-12-06 03:31:36 +03:00
|
|
|
</Heading>
|
|
|
|
<Flex justifyContent="space-between">
|
2019-01-26 01:38:34 +03:00
|
|
|
<Flex.Item flex="1 1 auto" is={Var}>
|
2018-12-06 03:31:36 +03:00
|
|
|
${hue}
|
|
|
|
</Flex.Item>
|
|
|
|
<Text fontFamily="mono">
|
2018-12-06 21:28:36 +03:00
|
|
|
{chroma(colorValue).css()}
|
2018-12-06 23:10:41 +03:00
|
|
|
{' / '}
|
|
|
|
{colorValue}
|
2018-12-06 03:31:36 +03:00
|
|
|
</Text>
|
|
|
|
</Flex>
|
|
|
|
{children}
|
|
|
|
</Box>
|
|
|
|
<Box bg={over}>
|
2018-12-06 21:28:36 +03:00
|
|
|
{values.map(swatchProps => (
|
|
|
|
<Swatch {...swatchProps} key={swatchProps.name} />
|
2018-12-06 03:31:36 +03:00
|
|
|
))}
|
|
|
|
</Box>
|
|
|
|
</Flex.Item>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:28:36 +03:00
|
|
|
FadeVariables.propTypes = {
|
|
|
|
bg: Box.propTypes.color,
|
|
|
|
color: Box.propTypes.color,
|
|
|
|
hue: PropTypes.oneOf(['black', 'white']),
|
2018-12-06 22:48:33 +03:00
|
|
|
over: PropTypes.string
|
2018-12-06 21:28:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function Swatch(props) {
|
|
|
|
const {name, value, textColor = overlayColor(value), ...rest} = props
|
2018-12-06 03:31:36 +03:00
|
|
|
return (
|
2019-01-18 03:04:14 +03:00
|
|
|
<Box bg={value} color={textColor} {...rest}>
|
2019-01-26 01:38:34 +03:00
|
|
|
<Text is={Flex} fontSize={1} justifyContent="space-between">
|
2018-12-06 03:31:36 +03:00
|
|
|
<Box p={3}>
|
2019-01-18 03:04:14 +03:00
|
|
|
<Var>${name}</Var>
|
2018-12-06 03:31:36 +03:00
|
|
|
</Box>
|
|
|
|
<Box p={3}>
|
2019-01-18 03:04:14 +03:00
|
|
|
<Text fontFamily="mono">{value}</Text>
|
2018-12-06 03:31:36 +03:00
|
|
|
</Box>
|
|
|
|
</Text>
|
|
|
|
</Box>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:28:36 +03:00
|
|
|
Swatch.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
textColor: PropTypes.string,
|
|
|
|
value: PropTypes.string.isRequired
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:31:36 +03:00
|
|
|
function Var(props) {
|
2018-12-06 21:28:36 +03:00
|
|
|
// FIXME: fontStyle should be a prop, right?
|
2019-01-26 01:38:34 +03:00
|
|
|
return <Text is="var" fontWeight="bold" fontFamily="mono" style={{fontStyle: 'normal'}} {...props} />
|
2018-12-06 21:28:36 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:48:33 +03:00
|
|
|
function overlayColor(bg) {
|
2018-12-08 01:36:38 +03:00
|
|
|
return chroma(bg).luminance() > 0.5 ? BLACK : WHITE
|
2018-12-06 21:28:36 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:48:33 +03:00
|
|
|
function fadeTextColor(fg, bg = WHITE) {
|
|
|
|
const rgb = compositeRGB(fg, bg)
|
|
|
|
return overlayColor(rgb)
|
2018-12-06 21:28:36 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:48:33 +03:00
|
|
|
/**
|
|
|
|
* Composite ("flatten") a foreground RGBA value with an RGB background into an
|
|
|
|
* RGB color for the purposes of measuring contrast or luminance.
|
|
|
|
*/
|
|
|
|
function compositeRGB(foreground, background) {
|
|
|
|
const [fr, fg, fb, fa] = chroma(foreground).rgba()
|
|
|
|
const [br, bg, bb] = chroma(background).rgb()
|
2018-12-08 01:36:38 +03:00
|
|
|
return chroma([(1 - fa) * br + fa * fr, (1 - fa) * bg + fa * fg, (1 - fa) * bb + fa * fb]).css()
|
2018-12-06 03:31:36 +03:00
|
|
|
}
|