Skip to content

feat(css): add color-mix() tool #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/color-mixer/classic.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

327 changes: 327 additions & 0 deletions tools/color-mixer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<link rel="stylesheet" href="classic.min.css" />
<script src="pickr.es5.min.js"></script>
<title>Color mixer: Utility to test color-mix() function</title>
<style>
#color-mixer {
font-family: sans-serif;
display: grid;
grid-template-columns: 150px 1fr 1fr 150px;
grid-template-areas:
"header header header header"
"text text text text"
"color-one-text mix-text mix-text color-two-text"
"color-picker-one mix-output mix-output color-picker-two"
"mix-output-text mix-output-text mix-output-text mix-output-text"
"percent-one percent-one percent-two percent-two"
"color-space-label color-space interpolation-label interpolation";
}

#color-mixer > * {
padding: 0;
margin: 0 auto;
}

#color-mixer > :nth-child(1) {
grid-area: header;
}

#color-mixer > :nth-child(2) {
grid-area: text;
margin-bottom: 2rem;
}

#color-mixer > :nth-child(3) {
grid-area: color-one-text;
}

#color-mixer > :nth-child(4) {
grid-area: mix-text;
}

#color-mixer > :nth-child(5) {
grid-area: color-two-text;
}

#color-mixer > :nth-child(6) {
grid-area: color-picker-one;
width: 100% !important;
height: 100px !important;
border-radius: 20% 0 0 20%;
cursor: pointer;
}

#color-mixer > :nth-child(7) {
grid-area: mix-output;
width: 100%;
height: 100px;
}

#color-mixer > :nth-child(8) {
grid-area: color-picker-two;
width: 100%;
height: 100px;
border-radius: 0 20% 20% 0;
cursor: pointer;
}

#color-mixer > :nth-child(9) {
grid-area: mix-output-text;
margin: 1rem auto;
font-weight: bold;
font-size: 0.8rem;
}

#color-mixer > :nth-child(10) {
grid-area: percent-one;
width: 100%;
display: flex;
}

#color-mixer > :nth-child(11) {
grid-area: percent-two;
width: 100%;
display: flex;
}

#color-mixer > :nth-child(12) {
grid-area: color-space-label;
margin: 0 0 0 auto;
}

#color-mixer > :nth-child(13) {
grid-area: color-space;
margin: 0 auto 0 1rem;
}

#color-mixer > :nth-child(14) {
grid-area: interpolation-label;
margin: 0 0 0 auto;
}

#color-mixer > :nth-child(15) {
grid-area: interpolation;
margin: 0 auto 0 1rem;
}

input[type="range"] {
width: 80%;
}

label {
font-family: monospace;
margin: 1rem;
}
</style>
</head>

<body>
<div id="color-mixer">
<h3>Color mixer</h3>
<p>Click on 'color-one' and 'color-two' to select colors.</p>

<div>color-one</div>
<div>mixed-color</div>
<div>color-two</div>

<div id="color-one" class="color-one"></div>
<div id="mixed-color"></div>
<div id="color-two" class="color-two"></div>

<label id="mix-output-text"></label>

<div>
<label id="percentage-one-label">50%</label>
<input id="percentage-one" type="range" name="percent1" step="0.1" />
</div>
<div>
<label id="percentage-two-label">50%</label>
<input id="percentage-two" type="range" name="percent2" step="0.1" />
</div>

<label>color space: </label>
<select id="color-space">
<option value="srgb">srgb</option>
<option value="srgb-linear">srgb-linear</option>
<option value="lab">lab</option>
<option value="oklab" selected>oklab</option>
<option value="xyz">xyz</option>
<option value="xyz-d50">xyz-d50</option>
<option value="xyz-d65">xyz-d65</option>
<option value="hsl">hsl</option>
<option value="hwb">hwb</option>
<option value="lch">lch</option>
<option value="oklch">oklch</option>
</select>

<label id="interpolation-method-label">interpolation method: </label>
<select id="interpolation-method">
<option value="shorter hue" selected>shorter hue</option>
<option value="longer hue">longer hue</option>
<option value="increasing hue">increasing hue</option>
<option value="decreasing hue">decreasing hue</option>
</select>
</div>

<script>
const polarColorSpaces = ["hsl", "hwb", "lch", "oklch"];
const root = document.querySelector(":root");

const colorSpace = document.getElementById("color-space");
const interpolationMethod = document.getElementById(
"interpolation-method"
);
const interpolationMethodLabel = document.getElementById(
"interpolation-method-label"
);

const colorOne = document.getElementById("color-one");
const colorTwo = document.getElementById("color-two");
const mixedColor = document.getElementById("mixed-color");
const mixedOutputText = document.getElementById("mix-output-text");

const colorTextOne = document.getElementById("color-one-text");
const colorTextTwo = document.getElementById("color-two-text");

const percentageOneLabel = document.getElementById(
"percentage-one-label"
);
const percentageTwoLabel = document.getElementById(
"percentage-two-label"
);
const percentageOne = document.getElementById("percentage-one");
const percentageTwo = document.getElementById("percentage-two");

let pickerOne, pickerTwo;

function init() {
const pickerOptions = {
el: ".color-one",
theme: "classic",
useAsButton: true,
default: "#ff7f50",
position: "bottom-middle",
components: {
preview: true,
opacity: true,
hue: true,
interaction: {
hex: true,
rgba: true,
hsla: true,
input: true,
clear: false,
save: true,
},
},
swatches: [
"rgba(244, 67, 54, 1)",
"rgba(233, 30, 99, 1)",
"rgba(156, 39, 176, 1)",
"rgba(103, 58, 183, 1)",
"rgba(63, 81, 181, 1)",
"rgba(33, 150, 243, 1)",
"rgba(3, 169, 244, 1)",
"rgba(0, 188, 212, 1)",
"rgba(0, 150, 136, 1)",
"rgba(76, 175, 80, 1)",
"rgba(139, 195, 74, 1)",
"rgba(205, 220, 57, 1)",
"rgba(255, 235, 59, 1)",
"rgba(255, 193, 7, 1)",
],
};

pickerOne = Pickr.create(pickerOptions);
pickerOne.setColor("#ff7f50");
colorOne.style.setProperty("background-color", "#ff7f50");
pickerOne.on("change", (color, source, instance) => {
colorOne.style.setProperty("background-color", color.toRGBA());
updateColorMix();
});

pickerOptions.el = ".color-two";
pickerOptions.default = "#00ffff";
pickerTwo = Pickr.create(pickerOptions);
pickerTwo.setColor("#00ffff");
colorTwo.style.setProperty("background-color", "#00ffff");
pickerTwo.on("change", (color, source, instance) => {
colorTwo.style.setProperty("background-color", color.toRGBA());
updateColorMix();
});

percentageOne.addEventListener("input", (e) => {
percentageOneLabel.innerText = e.target.value + "%";
updateColorMix();
});
percentageTwo.addEventListener("input", (e) => {
percentageTwoLabel.innerText = e.target.value + "%";
updateColorMix();
});
colorSpace.addEventListener("change", (e) => {
if (polarColorSpaces.includes(e.target.value)) {
interpolationMethod.style.visibility = "visible";
interpolationMethodLabel.style.visibility = "visible";
} else {
interpolationMethod.style.visibility = "hidden";
interpolationMethodLabel.style.visibility = "hidden";
}
updateColorMix();
});
interpolationMethod.addEventListener("change", (e) => {
updateColorMix();
});

interpolationMethod.style.visibility = "hidden";
interpolationMethodLabel.style.visibility = "hidden";
}

function updateColorMix() {
let colorMixFunction = "color-mix(in ";

root.style.setProperty("--color-space", colorSpace.value);
colorMixFunction += colorSpace.value;

if (polarColorSpaces.includes(colorSpace.value)) {
root.style.setProperty(
"--interpolation-method",
interpolationMethod.value
);
colorMixFunction += ` ${interpolationMethod.value}, `;
} else {
root.style.setProperty("--interpolation-method", "");
colorMixFunction += `, `;
}
root.style.setProperty(
"--color-one",
colorOne.style.getPropertyValue("background-color")
);
root.style.setProperty("--percentage-one", percentageOne.value + "%");
colorMixFunction += `${pickerOne.getColor().toHEXA().toString()} ${
percentageOne.value
}%, `;

root.style.setProperty(
"--color-two",
colorTwo.style.getPropertyValue("background-color")
);
root.style.setProperty("--percentage-two", percentageTwo.value + "%");
colorMixFunction += `${pickerTwo.getColor().toHEXA().toString()} ${
percentageTwo.value
}%)`;

mixedColor.style.setProperty("background-color", colorMixFunction);

colorMixFunction += ` = ${
window.getComputedStyle(mixedColor).backgroundColor
}`;
mixedOutputText.innerText = colorMixFunction;
}

init();
updateColorMix();
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions tools/color-mixer/pickr.es5.min.js

Large diffs are not rendered by default.