import React from 'react'
import PropTypes from 'prop-types'
import chroma from 'chroma-js'
import colors from 'primer-colors'
import titleCase from 'title-case'
import {BorderBox, Box, Flex, Heading, Text} from '@primer/components'
const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red']
const {black: BLACK, white: WHITE} = colors
export function ColorPalette(props) {
return (
{gradientHues.map(hue => {
const color = colors[hue][5]
return (
{titleCase(hue)}
)
})}
White
)
}
export function ColorVariables(props) {
return (
<>
{gradientHues.map(hue => (
))}
Black fades apply alpha transparency to the $black variable. The black color value has a slight
blue hue to match our grays.
White fades apply alpha transparency to the $white variable, below these are shown overlaid on
a dark gray background.
>
)
}
export function ColorVariable({hue, ...rest}) {
const values = colors[hue]
return (
{/* {titleCase(hue)} */}
{titleCase(hue)}
${hue}-500
{values[5]}
{values.map((value, i) => (
))}
)
}
ColorVariable.propTypes = {
hue: PropTypes.oneOf(Object.keys(colors)).isRequired
}
export function FadeVariables({hue, color, bg, over, children, ...rest}) {
const colorValue = colors[hue]
const alphas = [15, 30, 50, 70, 85]
const values = alphas.map(alpha => {
const value = chroma(colorValue)
.alpha(alpha / 100)
.css()
return {
name: `${hue}-fade-${alpha}`,
textColor: fadeTextColor(value, over),
value
}
})
const boxProps = {color, bg}
return (
{/* {titleCase(hue)} */}
{titleCase(hue)}
${hue}
{chroma(colorValue).css()}
{' / '}
{colorValue}
{children}
{values.map(swatchProps => (
))}
)
}
FadeVariables.propTypes = {
bg: Box.propTypes.color,
color: Box.propTypes.color,
hue: PropTypes.oneOf(['black', 'white']),
over: PropTypes.string
}
function Swatch(props) {
const {name, value, textColor = overlayColor(value), ...rest} = props
return (
${name}
{value}
)
}
Swatch.propTypes = {
name: PropTypes.string.isRequired,
textColor: PropTypes.string,
value: PropTypes.string.isRequired
}
function Var(props) {
// FIXME: fontStyle should be a prop, right?
return
}
function overlayColor(bg) {
return chroma(bg).luminance() > 0.5 ? BLACK : WHITE
}
function fadeTextColor(fg, bg = WHITE) {
const rgb = compositeRGB(fg, bg)
return overlayColor(rgb)
}
/**
* 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()
return chroma([(1 - fa) * br + fa * fr, (1 - fa) * bg + fa * fg, (1 - fa) * bb + fa * fb]).css()
}