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>RGB-HCT Converter</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
width: 80%;
margin: 40px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.color-preview {
width: 100px;
height: 100px;
border: 1px solid #ddd;
border-radius: 10px;
margin: 10px;
}
.slider-container {
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<h2>RGB-HCT Converter</h2>
<div class="color-preview" id="color-preview"></div>
<div class="slider-container">
<label for="red">Red:</label>
<input type="range" id="red" min="0" max="255" value="0">
<span id="red-value">0</span>
</div>
<div class="slider-container">
<label for="green">Green:</label>
<input type="range" id="green" min="0" max="255" value="0">
<span id="green-value">0</span>
</div>
<div class="slider-container">
<label for="blue">Blue:</label>
<input type="range" id="blue" min="0" max="255" value="0">
<span id="blue-value">0</span>
</div>
<div class="slider-container">
<label for="hue">Hue:</label>
<input type="range" id="hue" min="0" max="360" value="0">
<span id="hue-value">0</span>
</div>
<div class="slider-container">
<label for="chroma">Chroma:</label>
<input type="range" id="chroma" min="0" max="100" value="0">
<span id="chroma-value">0</span>
</div>
<div class="slider-container">
<label for="tone">Tone:</label>
<input type="range" id="tone" min="0" max="100" value="0">
<span id="tone-value">0</span>
</div>
<div>
<label for="hex">HEX:</label>
<input type="text" id="hex" value="#000000">
</div>
</div>
<script>
const redInput = document.getElementById('red');
const greenInput = document.getElementById('green');
const blueInput = document.getElementById('blue');
const hueInput = document.getElementById('hue');
const chromaInput = document.getElementById('chroma');
const toneInput = document.getElementById('tone');
const hexInput = document.getElementById('hex');
const colorPreview = document.getElementById('color-preview');
const redValue = document.getElementById('red-value');
const greenValue = document.getElementById('green-value');
const blueValue = document.getElementById('blue-value');
const hueValue = document.getElementById('hue-value');
const chromaValue = document.getElementById('chroma-value');
const toneValue = document.getElementById('tone-value');
function rgbToHct(r, g, b) {
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const delta = max - min;
let h, c, t;
if (delta === 0) {
h = 0;
} else if (max === r) {
h = (60 * ((g - b) / delta) + 360) % 360;
} else if (max === g) {
h = (60 * ((b - r) / delta) + 120) % 360;
} else {
h = (60 * ((r - g) / delta) + 240) % 360;
}
c = delta / 255;
t = (max + min) / 2 / 255;
return [h, c * 100, t * 100];
}
function hctToRgb(h, c, t) {
const hRad = h * Math.PI / 180;
const x = c * (1 - Math.abs((hRad / Math.PI) % 2 - 1));
let r, g, b;
if (hRad < Math.PI / 3) {
r = c;
g = x;
b = 0;
} else if (hRad < 2 * Math.PI / 3) {
r = x;
g = c;
b = 0;
} else if (hRad < Math.PI) {
r = 0;
g = c;
b = x;
} else if (hRad < 4 * Math.PI / 3) {
r = 0;
g = x;
b = c;
} else if (hRad < 5 * Math.PI / 3) {
r = x;
g = 0;
b = c;
} else {
r = c;
g = 0;
b = x;
}
const m = t - c / 2;
r = Math.round((r + m) * 255);
g = Math.round((g + m) * 255);
b = Math.round((b + m) * 255);
return [r, g, b];
}
function updateColor() {
const r = parseInt(redInput.value);
const g = parseInt(greenInput.value);
const b = parseInt(blueInput.value);
const h = parseInt(hueInput.value);
const c = parseInt(chromaInput.value) / 100;
const t = parseInt(toneInput.value) / 100;
const hex = rgbToHex(r, g, b);
hexInput.value = hex;
colorPreview.style.background = hex;
const hct = rgbToHct(r, g, b);
hueInput.value = hct[0];
chromaInput.value = hct[1];
toneInput.value = hct[2];
redValue.textContent = r;
greenValue.textContent = g;
blueValue.textContent = b;
hueValue.textContent = hct[0];
chromaValue.textContent = hct[1];
toneValue.textContent = hct[2];
}
function rgbToHex(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
redInput.addEventListener('input', updateColor);
greenInput.addEventListener('input', updateColor);
blueInput.addEventListener('input', updateColor);
hueInput.addEventListener('input', () => {
const h = parseInt(hueInput.value);
const c = parseInt(chromaInput.value) / 100;
const t = parseInt(toneInput.value) / 100;
const rgb = hctToRgb(h, c, t);
redInput.value = rgb[0];
greenInput.value = rgb[1];
blueInput.value = rgb[2];
updateColor();
});
chromaInput.addEventListener('input', () => {
const h = parseInt(hueInput.value);
const c = parseInt(chromaInput.value) / 100;
const t = parseInt(toneInput.value) / 100;
const rgb = hctToRgb(h, c, t);
redInput.value = rgb[0];
greenInput.value = rgb[1];
blueInput.value = rgb[2];
updateColor();
});
toneInput.addEventListener('input', () => {
const h = parseInt(hueInput.value);
const c = parseInt(chromaInput.value) / 100;
const t = parseInt(toneInput.value) / 100;
const rgb = hctToRgb(h, c, t);
redInput.value = rgb[0];
greenInput.value = rgb[1];
blueInput.value = rgb[2];
updateColor();
});
hexInput.addEventListener('input', () => {
const hex = hexInput.value;
const rgb = hexToRgb(hex);
if (rgb) {
redInput.value = rgb[0];
greenInput.value = rgb[1];
blueInput