html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Converter</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
.color-preview {
width: 100px;
height: 100px;
margin-bottom: 20px;
border: 1px solid #ccc;
}
.slider-container, .input-container {
display: flex;
gap: 10px;
margin-bottom: 10px;
align-items: center;
}
label {
width: 50px;
}
input[type="range"] {
flex: 1;
}
input[type="text"] {
width: 100px;
}
</style>
</head>
<body>
<div class="color-preview" id="colorPreview"></div>
<div class="slider-container">
<label for="r">R</label>
<input type="range" id="r" min="0" max="255">
<input type="text" id="rValue">
</div>
<div class="slider-container">
<label for="g">G</label>
<input type="range" id="g" min="0" max="255">
<input type="text" id="gValue">
</div>
<div class="slider-container">
<label for="b">B</label>
<input type="range" id="b" min="0" max="255">
<input type="text" id="bValue">
</div>
<div class="input-container">
<label for="hex">HEX</label>
<input type="text" id="hex">
</div>
<div class="slider-container">
<label for="hue">Hue</label>
<input type="range" id="hue" min="0" max="360">
<input type="text" id="hueValue">
</div>
<div class="slider-container">
<label for="chroma">Chroma</label>
<input type="range" id="chroma" min="0" max="150">
<input type="text" id="chromaValue">
</div>
<div class="slider-container">
<label for="tone">Tone</label>
<input type="range" id="tone" min="0" max="100">
<input type="text" id="toneValue">
</div>
<script>
class Hct {
constructor(argb) {
this.argb = argb;
const cam = Hct.camFromInt(argb);
this.hue = cam.hue;
this.chroma = cam.chroma;
this.tone = Hct.lstarFromArgb(argb);
}
static from(hue, chroma, tone) {
const argb = Hct.solveToInt(hue, chroma, tone);
return new Hct(argb);
}
static fromInt(argb) {
return new Hct(argb);
}
static solveToInt(hue, chroma, tone) {
const y = Hct.yFromLstar(tone);
const cam = { hue, chroma, y };
return Hct.argbFromCam(cam);
}
static camFromInt(argb) {
const r = (argb >> 16) & 255;
const g = (argb >> 8) & 255;
const b = argb & 255;
const y = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return { hue: (Math.atan2(g - b, r - g) * 180) / Math.PI, chroma: y, y };
}
static argbFromCam(cam) {
const r = Math.round(cam.y * Math.cos((cam.hue * Math.PI) / 180));
const g = Math.round(cam.y * Math.sin((cam.hue * Math.PI) / 180));
const b = Math.round(cam.chroma);
return (255 << 24) | (r << 16) | (g << 8) | b;
}
static yFromLstar(lstar) {
return (lstar + 16) / 116;
}
static lstarFromArgb(argb) {
const y = ((argb >> 16) & 255) * 0.2126 + ((argb >> 8) & 255) * 0.7152 + (argb & 255) * 0.0722;
return 116 * Math.pow(y, 1 / 3) - 16;
}
}
const preview = document.getElementById("colorPreview");
const rInput = document.getElementById("r");
const gInput = document.getElementById("g");
const bInput = document.getElementById("b");
const rValue = document.getElementById("rValue");
const gValue = document.getElementById("gValue");
const bValue = document.getElementById("bValue");
const hexInput = document.getElementById("hex");
const hueInput = document.getElementById("hue");
const chromaInput = document.getElementById("chroma");
const toneInput = document.getElementById("tone");
const hueValue = document.getElementById("hueValue");
const chromaValue = document.getElementById("chromaValue");
const toneValue = document.getElementById("toneValue");
function updatePreview(argb) {
const hex = "#" + argb.toString(16).padStart(8, "0").slice(2);
preview.style.backgroundColor = hex;
hexInput.value = hex;
}
function updateRgbInputs(argb) {
const r = (argb >> 16) & 255;
const g = (argb >> 8) & 255;
const b = argb & 255;
rInput.value = rValue.value = r;
gInput.value = gValue.value = g;
bInput.value = bValue.value = b;
}
function updateHctInputs(hct) {
hueInput.value = hueValue.value = Math.round(hct.hue);
chromaInput.value = chromaValue.value = Math.round(hct.chroma);
toneInput.value = toneValue.value = Math.round(hct.tone);
}
function argbFromRgb(r, g, b) {
return (255 << 24) | (r << 16) | (g << 8) | b;
}
function onRgbChange() {
const r = parseInt(rInput.value);
const g = parseInt(gInput.value);
const b = parseInt(bInput.value);
const argb = argbFromRgb(r, g, b);
const hct = Hct.fromInt(argb);
updatePreview(argb);
updateHctInputs(hct);
}
function onHexChange() {
const hex = hexInput.value.replace("#", "");
if (hex.length === 6) {
const argb = parseInt("ff" + hex, 16);
const hct = Hct.fromInt(argb);
updatePreview(argb);
updateRgbInputs(argb);
updateHctInputs(hct);
}
}
function onHctChange() {
const hue = parseFloat(hueInput.value);
const chroma = parseFloat(chromaInput.value);
const tone = parseFloat(toneInput.value);
const hct = Hct.from(hue, chroma, tone);
updatePreview(hct.argb);
updateRgbInputs(hct.argb);
}
[rInput, gInput, bInput].forEach(input => input.addEventListener("input", onRgbChange));
hexInput.addEventListener("input", onHexChange);
[hueInput, chromaInput, toneInput].forEach(input => input.addEventListener("input", onHctChange));
onRgbChange();
</script>
</body>
</html>