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: #f8f9fa;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
}
.color-preview {
width: 100px;
height: 100px;
margin-bottom: 10px;
}
.input-group {
display: flex;
flex-direction: column;
align-items: center;
background: #fff;
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
}
.input-group input[type="range"] {
width: 100%;
}
.input-group label {
font-size: 14px;
margin-bottom: 5px;
}
.input-group input[type="text"] {
width: 80px;
text-align: center;
}
</style>
</head>
<body>
<div class="color-preview" id="colorPreview"></div>
<div class="container">
<div class="input-group">
<label>R</label>
<input type="range" id="redRange" min="0" max="255">
<input type="text" id="redValue">
</div>
<div class="input-group">
<label>G</label>
<input type="range" id="greenRange" min="0" max="255">
<input type="text" id="greenValue">
</div>
<div class="input-group">
<label>B</label>
<input type="range" id="blueRange" min="0" max="255">
<input type="text" id="blueValue">
</div>
<div class="input-group">
<label>HEX</label>
<input type="text" id="hexValue">
</div>
<div class="input-group">
<label>Hue</label>
<input type="range" id="hueRange" min="0" max="360">
<input type="text" id="hueValue">
</div>
<div class="input-group">
<label>Chroma</label>
<input type="range" id="chromaRange" min="0" max="150">
<input type="text" id="chromaValue">
</div>
<div class="input-group">
<label>Tone</label>
<input type="range" id="toneRange" min="0" max="100">
<input type="text" id="toneValue">
</div>
</div>
<script>
// Helper functions
const clamp = (min, max, val) => Math.min(max, Math.max(min, val));
function rgbToHex(r, g, b) {
return `#${[r, g, b].map(v => v.toString(16).padStart(2, '0')).join('')}`;
}
function hexToRgb(hex) {
const bigint = parseInt(hex.slice(1), 16);
return {
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255
};
}
// HCT Conversion (using provided HCT class implementation)
class HCT {
static fromHueChromaTone(hue, chroma, tone) {
return new HCT(Hct.from(hue, chroma, tone).toInt());
}
static fromRgb(r, g, b) {
return new HCT(Hct.fromInt((255 << 24) | (r << 16) | (g << 8) | b));
}
constructor(argb) {
this.argb = argb;
const hct = Hct.fromInt(argb);
this.hue = hct.hue;
this.chroma = hct.chroma;
this.tone = hct.tone;
}
getRgb() {
return {
r: (this.argb >> 16) & 255,
g: (this.argb >> 8) & 255,
b: this.argb & 255
};
}
}
// DOM elements
const preview = document.getElementById('colorPreview');
const redRange = document.getElementById('redRange');
const greenRange = document.getElementById('greenRange');
const blueRange = document.getElementById('blueRange');
const redValue = document.getElementById('redValue');
const greenValue = document.getElementById('greenValue');
const blueValue = document.getElementById('blueValue');
const hexValue = document.getElementById('hexValue');
const hueRange = document.getElementById('hueRange');
const chromaRange = document.getElementById('chromaRange');
const toneRange = document.getElementById('toneRange');
const hueValue = document.getElementById('hueValue');
const chromaValue = document.getElementById('chromaValue');
const toneValue = document.getElementById('toneValue');
function updateFromRgb() {
const r = parseInt(redRange.value);
const g = parseInt(greenRange.value);
const b = parseInt(blueRange.value);
redValue.value = r;
greenValue.value = g;
blueValue.value = b;
const hex = rgbToHex(r, g, b);
hexValue.value = hex;
const hct = HCT.fromRgb(r, g, b);
hueRange.value = hueValue.value = Math.round(hct.hue);
chromaRange.value = chromaValue.value = Math.round(hct.chroma);
toneRange.value = toneValue.value = Math.round(hct.tone);
preview.style.backgroundColor = hex;
}
function updateFromHex() {
const hex = hexValue.value;
const { r, g, b } = hexToRgb(hex);
redRange.value = redValue.value = r;
greenRange.value = greenValue.value = g;
blueRange.value = blueValue.value = b;
const hct = HCT.fromRgb(r, g, b);
hueRange.value = hueValue.value = Math.round(hct.hue);
chromaRange.value = chromaValue.value = Math.round(hct.chroma);
toneRange.value = toneValue.value = Math.round(hct.tone);
preview.style.backgroundColor = hex;
}
function updateFromHct() {
const h = parseFloat(hueRange.value);
const c = parseFloat(chromaRange.value);
const t = parseFloat(toneRange.value);
hueValue.value = h;
chromaValue.value = c;
toneValue.value = t;
const hct = HCT.fromHueChromaTone(h, c, t);
const { r, g, b } = hct.getRgb();
redRange.value = redValue.value = r;
greenRange.value = greenValue.value = g;
blueRange.value = blueValue.value = b;
const hex = rgbToHex(r, g, b);
hexValue.value = hex;
preview.style.backgroundColor = hex;
}
// Event listeners
[redRange, greenRange, blueRange].forEach(range =>
range.addEventListener('input', updateFromRgb)
);
hexValue.addEventListener('input', updateFromHex);
[hueRange, chromaRange, toneRange].forEach(range =>
range.addEventListener('input', updateFromHct)
);
// Initialize default values
updateFromRgb();
</script>
<script src="https://cdn.jsdelivr.net/npm/material-color-utilities@0.0.1/typescript/hct/hct.js"></script>
</body>
</html>