-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adjust color lightness and create contrast chart (#2784)
resolves #2778 --------- Co-authored-by: Tobias Barsnes <[email protected]>
- Loading branch information
Showing
5 changed files
with
245 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.subHeading { | ||
margin-bottom: 12px !important; | ||
margin-top: 32px !important; | ||
} |
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,27 @@ | ||
'use client'; | ||
import { Heading } from '@digdir/designsystemet-react'; | ||
import { Container } from '@repo/components'; | ||
import { ContrastChart } from '../../components/ContrastChart/ContrastChart'; | ||
import classes from './page.module.css'; | ||
|
||
export default function ContrastCharts() { | ||
return ( | ||
<div> | ||
<main> | ||
<Container> | ||
<div> | ||
<Heading data-size='lg'>Kontrastoversikt</Heading> | ||
<Heading className={classes.subHeading} data-size='sm'> | ||
Light mode | ||
</Heading> | ||
<ContrastChart /> | ||
<Heading className={classes.subHeading} data-size='sm'> | ||
Dark mode | ||
</Heading> | ||
<ContrastChart type='dark' /> | ||
</div> | ||
</Container> | ||
</main> | ||
</div> | ||
); | ||
} |
103 changes: 103 additions & 0 deletions
103
apps/theme/components/ContrastChart/ContrastChart.module.css
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,103 @@ | ||
.color { | ||
height: 36px; | ||
display: block; | ||
flex: 1 1; | ||
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1); | ||
border-radius: 2px; | ||
} | ||
|
||
.colors { | ||
display: flex; | ||
gap: 12px; | ||
} | ||
|
||
.table { | ||
background-color: #e2e2e2; | ||
margin-bottom: 100px; | ||
} | ||
|
||
.cell { | ||
padding: 14px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
height: 100%; | ||
} | ||
|
||
.tag { | ||
background-color: #d23e3e; | ||
color: white; | ||
border-radius: 400px; | ||
font-size: 13px; | ||
font-weight: 500; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding: 3px 9px; | ||
} | ||
|
||
.AA { | ||
background-color: green; | ||
margin-left: 4px; | ||
} | ||
|
||
.AAA { | ||
background-color: #407be9; | ||
} | ||
|
||
.AA18 { | ||
background-color: #fbb025; | ||
color: black; | ||
} | ||
|
||
.meta { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
|
||
.contrast { | ||
font-size: 16px; | ||
font-weight: 500; | ||
width: 48px; | ||
text-align: center; | ||
} | ||
|
||
.table, | ||
.td, | ||
.th { | ||
border: 4px solid var(--ds-color-neutral-background-default); | ||
border-collapse: collapse; | ||
background-color: var(--ds-color-neutral-surface-default); | ||
} | ||
|
||
.th { | ||
height: 90px; | ||
width: 140px; | ||
padding: 16px; | ||
} | ||
|
||
.td { | ||
height: 90px; | ||
width: 140px; | ||
vertical-align: top; | ||
} | ||
|
||
.header { | ||
font-size: 16px; | ||
width: 30px; | ||
text-align: left; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2px; | ||
font-weight: 500; | ||
} | ||
|
||
.headerHex { | ||
color: var(--ds-color-neutral-text-subtle); | ||
font-weight: 400; | ||
} | ||
|
||
.contrastChart { | ||
background-color: transparent; | ||
} |
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,107 @@ | ||
import { | ||
type ColorInfo, | ||
generateThemeForColor, | ||
getColorNameFromNumber, | ||
getContrastFromHex, | ||
} from '@/packages/cli/dist/src'; | ||
|
||
import cl from 'clsx/lite'; | ||
import classes from './ContrastChart.module.css'; | ||
|
||
type ContrastChartProps = { | ||
type?: 'light' | 'dark'; | ||
}; | ||
|
||
export const ContrastChart = ({ type = 'light' }: ContrastChartProps) => { | ||
const theme = generateThemeForColor('#0062BA'); | ||
const includedColorIndexes = [1, 2, 3, 4, 5, 6, 7, 8, 12, 13]; | ||
const reducedLight = theme.light.filter((color) => | ||
includedColorIndexes.includes(color.number), | ||
); | ||
const reducedDark = theme.dark.filter((color) => | ||
includedColorIndexes.includes(color.number), | ||
); | ||
|
||
const Tag = ({ | ||
color1, | ||
color2, | ||
}: { color1: ColorInfo; color2: ColorInfo }) => { | ||
const contrast = getContrastFromHex(color1.hex, color2.hex); | ||
let type = 'AAA'; | ||
|
||
if (contrast < 3) { | ||
type = 'FAIL'; | ||
} else if (contrast < 4.5) { | ||
type = 'AA18'; | ||
} else if (contrast < 7) { | ||
type = 'AA'; | ||
} | ||
|
||
return <div className={cl(classes.tag, classes[type])}>{type}</div>; | ||
}; | ||
|
||
const TdCell = ({ | ||
color1, | ||
color2, | ||
}: { color1: ColorInfo; color2: ColorInfo }) => { | ||
return ( | ||
<div className={classes.cell}> | ||
<div className={classes.colors}> | ||
<div | ||
className={classes.color} | ||
style={{ backgroundColor: color2.hex }} | ||
></div> | ||
<div | ||
className={classes.color} | ||
style={{ backgroundColor: color1.hex }} | ||
></div> | ||
</div> | ||
<div className={classes.meta}> | ||
<Tag color1={color1} color2={color2} /> | ||
<div className={classes.contrast}> | ||
{Math.floor(getContrastFromHex(color1.hex, color2.hex) * 10) / 10} | ||
:1 | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const ThCell = ({ color }: { color: ColorInfo }) => { | ||
return ( | ||
<th className={classes.th}> | ||
<div className={classes.header}> | ||
{getColorNameFromNumber(color.number)} | ||
<div className={classes.headerHex}>{color.hex}</div> | ||
</div> | ||
</th> | ||
); | ||
}; | ||
|
||
const reducedColors = type === 'light' ? reducedLight : reducedDark; | ||
const themeColors = type === 'light' ? theme.light : theme.dark; | ||
|
||
return ( | ||
<div data-ds-color-mode={type} className={classes.contrastChart}> | ||
<table className={classes.table}> | ||
<tr> | ||
<th /> | ||
{reducedColors.map((color, index) => ( | ||
<ThCell key={index} color={reducedColors[index]} /> | ||
))} | ||
</tr> | ||
|
||
{includedColorIndexes.map((number, index) => ( | ||
<tr key={index}> | ||
<ThCell color={reducedColors[index]} /> | ||
{reducedColors.map((color, index) => ( | ||
<td key={index} className={classes.td}> | ||
<TdCell color1={color} color2={themeColors[number - 1]} /> | ||
</td> | ||
))} | ||
</tr> | ||
))} | ||
</table> | ||
</div> | ||
); | ||
}; |
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