File size: 7,975 Bytes
85f2ebb
 
 
 
 
c5b8765
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
 
 
c5b8765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
85f2ebb
 
c5b8765
85f2ebb
c5b8765
85f2ebb
c5b8765
 
 
 
 
 
85f2ebb
c5b8765
 
 
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
 
 
 
85f2ebb
c5b8765
 
 
 
 
85f2ebb
c5b8765
 
 
85f2ebb
c5b8765
 
 
 
 
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85f2ebb
c5b8765
85f2ebb
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!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>