html Copy code
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: system-ui;
max-width: 800px;
margin: 40px auto;
padding: 20px;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.preview {
width: 100%;
height: 200px;
border-radius: 8px;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
input[type="text"],
input[type="number"] {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="range"] {
width: 100%;
}
.value-display {
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<div>
<div class="preview" id="colorPreview"></div>
<div class="input-group">
<label>RGB</label>
<input type="text" id="rgbInput" placeholder="rgb(255,255,255)">
</div>
<div class="input-group">
<label>HEX</label>
<input type="text" id="hexInput" placeholder="#FFFFFF">
</div>
</div>
<div>
<div class="input-group">
<label>Hue <span class="value-display" id="hueValue">0</span></label>
<input type="range" id="hueSlider" min="0" max="360" value="0">
</div>
<div class="input-group">
<label>Chroma <span class="value-display" id="chromaValue">0</span></label>
<input type="range" id="chromaSlider" min="0" max="150" value="0">
</div>
<div class="input-group">
<label>Tone <span class="value-display" id="toneValue">100</span></label>
<input type="range" id="toneSlider" min="0" max="100" value="100">
</div>
</div>
</div>
<script>
class ColorConverter {
static rgbToHex(r, g, b) {
return '#' + [r, g, b].map(x => {
const hex = x.toString(16);
return hex.length === 1 ? '0' + hex : hex;
}).join('');
}
static hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
static linearized(rgbComponent) {
const normalized = rgbComponent / 255.0;
if (normalized <= 0.040449936) {
return normalized / 12.92 * 100.0;
} else {
return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
}
}
static delinearized(rgbComponent) {
const normalized = rgbComponent / 100.0;
let delinearized = 0.0;
if (normalized <= 0.0031308) {
delinearized = normalized * 12.92;
} else {
delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
}
return Math.max(0, Math.min(255, Math.round(delinearized * 255.0)));
}
}
const preview = document.getElementById('colorPreview');
const rgbInput = document.getElementById('rgbInput');
const hexInput = document.getElementById('hexInput');
const hueSlider = document.getElementById('hueSlider');
const chromaSlider = document.getElementById('chromaSlider');
const toneSlider = document.getElementById('toneSlider');
const hueValue = document.getElementById('hueValue');
const chromaValue = document.getElementById('chromaValue');
const toneValue = document.getElementById('toneValue');
function updateFromRgb(r, g, b) {
const hex = ColorConverter.rgbToHex(r, g, b);
hexInput.value = hex;
rgbInput.value = `rgb(${r},${g},${b})`;
preview.style.backgroundColor = hex;
}
function updateFromHex(hex) {
const rgb = ColorConverter.hexToRgb(hex);
if (rgb) {
rgbInput.value = `rgb(${rgb.r},${rgb.g},${rgb.b})`;
preview.style.backgroundColor = hex;
}
}
function updateFromHct(h, c, t) {
// Simplified HCT to RGB conversion
const s = c / 100;
const l = t / 100;
let r, g, b;
if (c === 0) {
r = g = b = Math.round(t * 2.55);
} else {
const hue = h / 60;
const x = s * (1 - Math.abs((hue % 2) - 1));
const m = l - (s / 2);
let r1, g1, b1;
if (hue >= 0 && hue < 1) { r1 = s; g1 = x; b1 = 0; }
else if (hue >= 1 && hue < 2) { r1 = x; g1 = s; b1 = 0; }
else if (hue >= 2 && hue < 3) { r1 = 0; g1 = s; b1 = x; }
else if (hue >= 3 && hue < 4) { r1 = 0; g1 = x; b1 = s; }
else if (hue >= 4 && hue < 5) { r1 = x; g1 = 0; b1 = s; }
else { r1 = s; g1 = 0; b1 = x; }
r = Math.round((r1 + m) * 255);
g = Math.round((g1 + m) * 255);
b = Math.round((b1 + m) * 255);
}
updateFromRgb(r, g, b);
}
rgbInput.addEventListener('change', (e) => {
const match = e.target.value.match(/rgb\((\d+),(\d+),(\d+)\)/);
if (match) {
updateFromRgb(parseInt(match[1]), parseInt(match[2]), parseInt(match[3]));
}
});
hexInput.addEventListener('change', (e) => {
updateFromHex(e.target.value);
});
[hueSlider, chromaSlider, toneSlider].forEach(slider => {
slider.addEventListener('input', (e) => {
const h = parseInt(hueSlider.value);
const c = parseInt(chromaSlider.value);
const t = parseInt(toneSlider.value);
hueValue.textContent = h;
chromaValue.textContent = c;
toneValue.textContent = t;
updateFromHct(h, c, t);
});
});
//