dev-tools / color.html
natep2234's picture
Upload color.html with huggingface_hub
c5b8765 verified
Raw History Blame Contribute Delete
7.98 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Converter — dev.tools</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #0a0a0a;
color: #c8c0b0;
font-family: 'Courier New', Courier, monospace;
min-height: 100vh;
padding: 2rem;
line-height: 1.6;
}
h1 { font-size: 1.8rem; color: #d4a54a; margin-bottom: 0.3rem; }
.subtitle { color: #665e50; font-size: 0.85rem; margin-bottom: 2rem; }
.input-row {
display: flex;
gap: 1rem;
margin-bottom: 1.5rem;
flex-wrap: wrap;
}
.input-group { flex: 1; min-width: 200px; }
.input-group label {
display: block;
color: #d4a54a;
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.4rem;
}
input[type="text"] {
width: 100%;
background: #141414;
color: #c8c0b0;
border: 1px solid #2a2520;
border-radius: 4px;
padding: 0.7rem 1rem;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
outline: none;
}
input[type="text"]:focus { border-color: #d4a54a; }
input[type="text"]::placeholder { color: #44403a; }
.preview-area {
display: flex;
gap: 1.5rem;
margin-bottom: 1.5rem;
align-items: stretch;
flex-wrap: wrap;
}
.color-swatch {
width: 160px;
height: 160px;
border-radius: 8px;
border: 1px solid #2a2520;
transition: background-color 0.15s;
flex-shrink: 0;
}
.color-info {
flex: 1;
min-width: 200px;
}
.section {
background: #141414;
border: 1px solid #2a2520;
border-radius: 4px;
padding: 1rem;
margin-bottom: 0.8rem;
}
.section-title {
color: #d4a54a;
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
.val { color: #a3be8c; cursor: pointer; }
.val:hover { text-decoration: underline; }
.copied {
color: #d4a54a;
font-size: 0.75rem;
opacity: 0;
transition: opacity 0.2s;
margin-left: 0.5rem;
}
.copied.show { opacity: 1; }
.contrast-row { display: flex; gap: 1rem; margin-top: 0.5rem; }
.contrast-sample {
padding: 0.4rem 0.8rem;
border-radius: 3px;
font-size: 0.85rem;
}
.brand {
text-align: center;
color: #2a2520;
font-size: 0.75rem;
margin-top: 3rem;
}
</style>
</head>
<body>
<h1>Color Converter</h1>
<p class="subtitle">Enter any format. See all formats.</p>
<div class="input-row">
<div class="input-group">
<label>Hex</label>
<input type="text" id="hexInput" placeholder="#d4a54a">
</div>
<div class="input-group">
<label>RGB</label>
<input type="text" id="rgbInput" placeholder="212, 165, 74">
</div>
<div class="input-group">
<label>HSL</label>
<input type="text" id="hslInput" placeholder="39, 64%, 56%">
</div>
</div>
<div class="preview-area">
<div class="color-swatch" id="swatch"></div>
<div class="color-info">
<div class="section">
<div class="section-title">All Formats</div>
<div>hex: <span class="val" id="outHex" onclick="copy(this)"></span><span class="copied" id="copiedHex">copied</span></div>
<div>rgb: <span class="val" id="outRgb" onclick="copy(this)"></span><span class="copied" id="copiedRgb">copied</span></div>
<div>hsl: <span class="val" id="outHsl" onclick="copy(this)"></span><span class="copied" id="copiedHsl">copied</span></div>
<div>css: <span class="val" id="outCss" onclick="copy(this)"></span><span class="copied" id="copiedCss">copied</span></div>
</div>
<div class="section">
<div class="section-title">Contrast</div>
<div class="contrast-row">
<span class="contrast-sample" id="contrastBlack">Black text</span>
<span class="contrast-sample" id="contrastWhite">White text</span>
</div>
<div style="margin-top:0.5rem;font-size:0.85rem">
Luminance: <span id="lumVal"></span> — use <span id="recommendation"></span> text
</div>
</div>
</div>
</div>
<div class="brand">dev.tools by Atreus & Loki</div>
<script>
let updating = false;
function clamp(v, min, max) { return Math.max(min, Math.min(max, v)); }
function hexToRgb(hex) {
hex = hex.replace(/^#/, '');
if (hex.length === 3) hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
if (hex.length !== 6 || !/^[0-9a-fA-F]{6}$/.test(hex)) return null;
return [parseInt(hex.slice(0,2),16), parseInt(hex.slice(2,4),16), parseInt(hex.slice(4,6),16)];
}
function rgbToHex(r,g,b) {
return '#' + [r,g,b].map(v => clamp(v,0,255).toString(16).padStart(2,'0')).join('');
}
function rgbToHsl(r,g,b) {
r/=255; g/=255; b/=255;
const max=Math.max(r,g,b), min=Math.min(r,g,b), d=max-min;
let h=0, s=0, l=(max+min)/2;
if (d !== 0) {
s = l > 0.5 ? d/(2-max-min) : d/(max+min);
if (max===r) h=((g-b)/d+(g<b?6:0))/6;
else if (max===g) h=((b-r)/d+2)/6;
else h=((r-g)/d+4)/6;
}
return [Math.round(h*360), Math.round(s*100), Math.round(l*100)];
}
function hslToRgb(h,s,l) {
h/=360; s/=100; l/=100;
if (s===0) { const v=Math.round(l*255); return [v,v,v]; }
const hue2rgb=(p,q,t)=>{
if(t<0)t+=1;if(t>1)t-=1;
if(t<1/6)return p+(q-p)*6*t;
if(t<1/2)return q;
if(t<2/3)return p+(q-p)*(2/3-t)*6;
return p;
};
const q=l<0.5?l*(1+s):l+s-l*s, p=2*l-q;
return [Math.round(hue2rgb(p,q,h+1/3)*255), Math.round(hue2rgb(p,q,h)*255), Math.round(hue2rgb(p,q,h-1/3)*255)];
}
function luminance(r,g,b) {
const [rs,gs,bs] = [r,g,b].map(v => { v/=255; return v<=0.03928?v/12.92:Math.pow((v+0.055)/1.055,2.4); });
return 0.2126*rs + 0.7152*gs + 0.0722*bs;
}
function updateAll(r,g,b, source) {
if (updating) return;
updating = true;
const hex = rgbToHex(r,g,b);
const [h,s,l] = rgbToHsl(r,g,b);
if (source !== 'hex') document.getElementById('hexInput').value = hex;
if (source !== 'rgb') document.getElementById('rgbInput').value = `${r}, ${g}, ${b}`;
if (source !== 'hsl') document.getElementById('hslInput').value = `${h}, ${s}%, ${l}%`;
document.getElementById('swatch').style.backgroundColor = hex;
document.getElementById('outHex').textContent = hex;
document.getElementById('outRgb').textContent = `rgb(${r}, ${g}, ${b})`;
document.getElementById('outHsl').textContent = `hsl(${h}, ${s}%, ${l}%)`;
document.getElementById('outCss').textContent = hex;
const lum = luminance(r,g,b);
document.getElementById('contrastBlack').style.backgroundColor = hex;
document.getElementById('contrastBlack').style.color = '#000';
document.getElementById('contrastWhite').style.backgroundColor = hex;
document.getElementById('contrastWhite').style.color = '#fff';
document.getElementById('lumVal').textContent = lum.toFixed(3);
document.getElementById('recommendation').textContent = lum > 0.179 ? 'black' : 'white';
updating = false;
}
document.getElementById('hexInput').addEventListener('input', function() {
const rgb = hexToRgb(this.value);
if (rgb) updateAll(...rgb, 'hex');
});
document.getElementById('rgbInput').addEventListener('input', function() {
const m = this.value.match(/(\d{1,3})\s*[,\s]\s*(\d{1,3})\s*[,\s]\s*(\d{1,3})/);
if (m) updateAll(parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), 'rgb');
});
document.getElementById('hslInput').addEventListener('input', function() {
const m = this.value.match(/(\d{1,3})\s*[,\s]\s*(\d{1,3})%?\s*[,\s]\s*(\d{1,3})%?/);
if (m) {
const rgb = hslToRgb(parseInt(m[1]), parseInt(m[2]), parseInt(m[3]));
updateAll(...rgb, 'hsl');
}
});
function copy(el) {
navigator.clipboard.writeText(el.textContent);
const id = 'copied' + el.id.replace('out','');
const badge = document.getElementById(id);
badge.classList.add('show');
setTimeout(() => badge.classList.remove('show'), 1000);
}
updateAll(212, 165, 74, '');
</script>
</body>
</html>