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>Konverter Warna</title>
<style>
body { font-family: sans-serif; display: flex; flex-direction: column; align-items: center; gap: 1rem; padding: 1rem; }
#preview { width: 100px; height: 100px; border: 1px solid black; }
.slider-group { display: flex; flex-direction: column; gap: 0.5rem; }
input[type="range"] { width: 200px; }
input[type="number"] { width: 50px; }
.input-group { display: flex; align-items: center; gap: 0.5rem;}
#hex { width: 100px;}
</style>
</head>
<body>
<div id="preview"></div>
<div class="slider-group">
<div class="input-group">R: <input type="range" id="r" min="0" max="255" value="255"><input type="number" id="rVal" min="0" max="255" value="255"></div>
<div class="input-group">G: <input type="range" id="g" min="0" max="255" value="0"><input type="number" id="gVal" min="0" max="255" value="0"></div>
<div class="input-group">B: <input type="range" id="b" min="0" max="255" value="0"><input type="number" id="bVal" min="0" max="255" value="0"></div>
HEX: <input type="text" id="hex" value="#ff0000">
</div>
<div class="slider-group">
<div class="input-group">H: <input type="range" id="h" min="0" max="360" value="0"><input type="number" id="hVal" min="0" max="360" value="0"></div>
<div class="input-group">C: <input type="range" id="c" min="0" max="200" value="100"><input type="number" id="cVal" min="0" max="200" value="100"></div>
<div class="input-group">T: <input type="range" id="t" min="0" max="100" value="50"><input type="number" id="tVal" min="0" max="100" value="50"></div>
</div>
<script src="https://unpkg.com/material-color-utilities@0.2.0/dist/index.umd.js"></script>
<script>
const preview = document.getElementById('preview');
const r = document.getElementById('r'); const rVal = document.getElementById('rVal');
const g = document.getElementById('g'); const gVal = document.getElementById('gVal');
const b = document.getElementById('b'); const bVal = document.getElementById('bVal');
const hex = document.getElementById('hex');
const h = document.getElementById('h'); const hVal = document.getElementById('hVal');
const c = document.getElementById('c'); const cVal = document.getElementById('cVal');
const t = document.getElementById('t'); const tVal = document.getElementById('tVal');
function updatePreview(argb) {
preview.style.backgroundColor = `#${(argb & 0xffffff).toString(16).padStart(6, '0')}`;
}
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;
}
[r, g, b, rVal, gVal, bVal, hex, h, c, t, hVal, cVal, tVal].forEach(el => el.addEventListener('input', () => {
if (el.id.startsWith('r') || el.id.startsWith('g') || el.id.startsWith('b') || el.id === 'hex') {
let rgb = [r.value, g.value, b.value].map(Number);
if (el.id === 'hex') rgb = hexToRgb(hex.value);
if (!rgb) return;
const argb = (255 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
const hct = material.Hct.fromInt(argb);
[rVal.value, gVal.value, bVal.value] = rgb;
[h.value, c.value, t.value] = [hct.hue, hct.chroma, hct.tone];
[hVal.value, cVal.value, tVal.value] = [hct.hue, hct.chroma, hct.tone];
hex.value = rgbToHex(...rgb);
updatePreview(argb)
} else {
const hct = material.Hct.from(h.value, c.value, t.value);
const argb = hct.toInt();
updatePreview(argb);
const rgb = [material.ColorUtils.redFromArgb(argb), material.ColorUtils.greenFromArgb(argb), material.ColorUtils.blueFromArgb(argb)];
[r.value, g.value, b.value] = rgb;
[rVal.value, gVal.value, bVal.value] = rgb;
hex.value = rgbToHex(...rgb);
[hVal.value, cVal.value, tVal.value] = [hct.hue, hct.chroma, hct.tone];
}
}));
</script>
</body>
</html>