import * as React from 'react'; import { ThemeProvider, createTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import Typography from '@mui/material/Typography'; import Slider from '@mui/material/Slider'; const theme = createTheme({ cssVariables: { nativeColor: true, cssVarPrefix: 'contrast', // This is for the demo only, you don't need to set this to use the feature }, }); export default function ContrastTextDemo() { const [lightness, setLightness] = React.useState(0.65); const [chroma, setChroma] = React.useState(0.3); const [hue, setHue] = React.useState(29); // Create OKLCH color from slider values const backgroundColor = `oklch(${lightness} ${chroma} ${hue})`; // Get contrast text using theme function const contrastText = theme.palette.getContrastText(backgroundColor); return ( {/* Live Preview Square */} {backgroundColor} {/* Sliders */} OKLCH
Lightness: {lightness} setLightness(value)} min={0} max={1} step={0.01} valueLabelDisplay="auto" />
Chroma: {chroma} setChroma(value)} min={0} max={0.4} step={0.01} valueLabelDisplay="auto" />
Hue: {hue}° setHue(value)} min={0} max={360} step={1} valueLabelDisplay="auto" />
Text color: {contrastText}
); }