Created
July 22, 2022 17:00
-
-
Save caraya/d4e81bc332bbf25659e424e52fb67d96 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<style> | |
.box { | |
height: 100px; | |
width: 100px; | |
border: 5px solid #000; | |
} | |
.rp { | |
background-color: var(--color-rebeccapurple); | |
} | |
.magenta { | |
background-color: var(--color-magenta); | |
} | |
.red { | |
background-color: var(--color-red); | |
} | |
</style> | |
</head> | |
<body> | |
<div class="box rp"></div> | |
<div class="box magenta"></div> | |
<div class="box red"></div> | |
<script type="module"> | |
import Color from "https://colorjs.io/dist/color.js"; | |
function processColor(name, color) { | |
let myColor = new Color(color); | |
let cssColor; | |
if (CSS.supports("color", myColor)) { | |
cssColor = myColor.toString(); | |
} | |
// Check if the browser supports the display-p3 color space | |
// if it does then convert the color to that color space | |
if (CSS.supports("color", "color(display-p3 0 0 0)")) { | |
cssColor = myColor.to("p3").toString(); | |
} | |
if (CSS.supports("color", "lch(0% 0 0)")) { | |
cssColor = myColor.to("lch").toString(); | |
} | |
let root = document.documentElement; | |
let colorName = ('--color-' + name).toString(); | |
let registeredColor = root.style.setProperty(colorName, cssColor); | |
return registeredColor | |
} | |
processColor('rebeccapurple', '#663399') | |
processColor('magenta', '#ff00ff') | |
processColor('red', '#ff0000') | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment