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>
<label for="rgb">RGB:</label>
<input type="text" id="rgb" value="255, 0, 0">
<button id="rgb-to-hct">Convert to HCT</button>
</div>
<div>
<label for="hct">HCT:</label>
<input type="text" id="hct" value="0, 100, 100">
<button id="hct-to-rgb">Convert to RGB</button>
</div>
<div>
<label for="hex">HEX:</label>
<input type="text" id="hex" value="#ff0000">
</div>
<div class="color-preview" id="color-preview"></div>
<div class="slider-container">
<label for="hue">Hue:</label>
<input type="range" id="hue" min="0" max="360" value="0">
<label for="chroma">Chroma:</label>
<input type="range" id="chroma" min="0" max="100" value="100">
<label for="tone">Tone:</label>
<input type="range" id="tone" min="0" max="100" value="100">
</div>
</div>
<script>
const rgbInput = document.getElementById('rgb');
const hctInput = document.getElementById('hct');
const hexInput = document.getElementById('hex');
const colorPreview = document.getElementById('color-preview');
const hueSlider = document.getElementById('hue');
const chromaSlider = document.getElementById('chroma');
const toneSlider = document.getElementById('tone');
function rgbToHct(rgb) {
const [r, g, b] = rgb.split(',').map(Number);
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 = (max + min) / 2;
t = delta / (1 - Math.abs(2 * c - 1));
return [h, c, t];
}
function hctToRgb(hct) {
const [h, c, t] = hct.split(',').map(Number);
const x = c * (1 - Math.abs((h / 60) % 2 - 1));
const m = c - x;
let r, g, b;
if (h < 60) {
r = c;
g = x;
b = 0;
} else if (h < 120) {
r = x;
g = c;
b = 0;
} else if (h < 180) {
r = 0;
g = c;
b = x;
} else if (h < 240) {
r = 0;
g = x;
b = c;
} else if (h < 300) {
r = x;
g = 0;
b = c;
} else {
r = c;
g = 0;
b = x;
}
r = Math.round((r + m) * 255);
g = Math.round((g + m) * 255);
b = Math.round((b + m) * 255);
return [r, g, b];
}
function rgbToHex(rgb) {
const [r, g, b] = rgb.split(',').map(Number);
return `#${r.[Redacted InternationalBankingAccountNumber]{g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
}
function hexToRgb(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}
rgbInput.addEventListener('input', () => {
const rgb = rgbInput.value;
const hct = rgbToHct(rgb);
hctInput.value = hct.join(', ');
const hex = rgbToHex(rgb);
hexInput.value = hex;
colorPreview.style.backgroundColor = hex;
});
hctInput.addEventListener('input', () => {
const hct = hctInput.value;
const rgb = hctToRgb(hct);
rgbInput.value = rgb.join(', ');
const hex = rgbToHex(rgb.join(', '));
hexInput.value = hex;
colorPreview.style.backgroundColor = hex;
});
hexInput.addEventListener('input', () => {
const hex = hexInput.value;
const rgb = hexToRgb(hex);
rgbInput.value = rgb.join(', ');
const hct = rgbToHct(rgb.join(', '));
hctInput.value = hct.join(', ');
colorPreview.style.backgroundColor = hex;
});
hueSlider.addEventListener('input', () => {
const hct = hctInput.value.split(',').map(Number);
hct[0] = parseInt(hueSlider.value);
hctInput.value = hct.join(', ');
const rgb = hctToRgb(hct.join(', '));
rgbInput.value = rgb.join(', ');
const hex = rgbToHex(rgb.join(', '));
hexInput.value = hex;
colorPreview.style.backgroundColor = hex;
});
chromaSlider.addEventListener('input', () => {
const hct = hctInput.value.split(',').map(Number);
hct[1] = parseInt(chromaSlider.value);
hctInput.value = hct.join(', ');
const rgb = hctToRgb(hct.join(', '));
rgbInput.value = rgb.join(', ');
const hex = rgbToHex(rgb.join(', '));
hexInput.value = hex;
colorPreview.style.backgroundColor = hex;
});
toneSlider.addEventListener('input', () => {
const hct = hctInput.value.split(',').map(Number);
hct[2] = parseInt(toneSlider.value);
hctInput.value = hct.join(', ');
const rgb = hctToRgb(hct.join(', '));
rgbInput.value = rgb.join(', ');
const hex = rgbToHex(rgb.join(', '));
hexInput.value = hex;
colorPreview.style.backgroundColor = hex;
});
</script>
</body>
</html>