<input id="decompose" class="decompose" type="checkbox" checked>
<label for="decompose"> decompose (r+g+b)</label>
<div class="result">
<div>rgb(<span class="value-r">255</span> <span class="value-g">255</span> <span class="value-b">255</span>)</div>
<div>#<span class="value-rhex">ff</span> <span class="value-ghex">ff</span> <span class="value-bhex">ff</span></div></div>
<div class="colors">
<span id="red" class="r color"></span>
<span id="green" class="g color"></span>
<span id="blue" class="b color"></span>
</div>
<div class="panel">
<label class="label-r">
<span>decimal: <span id="value-r" class="value value-r">255</span></span>
<span>hex: <span id="value-rhex" class="value value-rhex">ff</span></span>
<span><span id="value-rhex-16" class="i16">15</span> * 16 + <span id="value-rhex-1" class="i1">15</span></span>
<input id="r" data-target="r" type="range" step="1" min="0" max="255" value="255">
</label>
<label class="label-g">
<span>decimal: <span id="value-g" class="value value-g">255</span></span>
<span>hex: <span id="value-ghex" class="value value-ghex">ff</span> </span>
<span><span id="value-ghex-16" class="i16">15</span> * 16 + <span id="value-ghex-1" class="i1">15</span></span>
<input id="g" data-target="g" type="range" step="1" min="0" max="255" value="255">
</label>
<label class="label-b">
<span>decimal: <span id="value-b" class="value value-b">255</span></span>
<span>hex: <span id="value-bhex" class="value value-bhex">ff</span></span>
<span><span id="value-bhex-16" class="i16">15</span> * 16 + <span id="value-bhex-1" class="i1">15</span></span>
<input id="b" data-target="b" type="range" step="1" min="0" max="255" value="255">
</label>
</div>
:root {
--size: 45vw;
}
html {
font-family: Source Code Pro, sans-serif;
box-sizing: border-box;
}
* {
box-sizing: inherit;
}
body {
line-height: 1.5;
height: 100vh;
margin: 0;
overflow: hidden;
background: #000;
color: whitesmoke;
font-size: .875rem;
padding: 1rem;
}
.colors {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
}
.color {
position: absolute;
width: var(--size);
height: var(--size);
left: 0;
right:0;
top: 0;
bottom: 0;
margin: auto;
mix-blend-mode: screen;
border-radius: 50%;
transition: transform 1s, border-radius 1s, color .5s;
}
.r {
background: #f00;
}
.g {
background: #0f0;
}
.b {
background: #00f;
}
.panel {
position: absolute;
height: 100vh;
width: 100%;
top: 0;
left: 0;
}
.panel label {
position: absolute;
padding: 1vw;
}
label > span {
display: block;
}
.label-r {
color: white;
background: red;
top: 50%;
left: 50%;
transform: translateY(calc(-0.75 * var(--size))) translateX(calc(-0.75 * var(--size)));
}
.label-g {
background: lime;
color: black;
top: 50%;
right: 50%;
transform: translateY(calc(-0.75 * var(--size))) translateX(calc(0.75 * var(--size)));
}
.label-b {color: white;
background: blue;
bottom: 50%;
left: 50%;
transform: translateY(calc(0.75 * var(--size))) translateX(calc(-0.475 * var(--size)));
}
.decompose:checked ~ .colors .r {
transform: translate3d(-25%, -25%, 0);
border-radius: 0 50% 50% 50%;
}
.decompose:checked ~ .colors .g {
transform: translate3d(25%, -25%, 0);
border-radius: 50% 0 50% 50%;
}
.decompose:checked ~ .colors .b {
transform: translate3d(0, 25%, 0);
border-radius: 50% 50% 50% 0;
}
input[type="checkbox"],
label[for=decompose] {
position: relative;
z-index: 1;
}
.result {
font-size: 1rem;
bottom: .5rem;
left: 0.5rem;
background: black;
position: absolute;
z-index: 1;
padding: .5rem;
}
.result div {
margin-bottom: .5rem;
}
.result div + div {
padding-left: 3ch;
margin: 0;
}
.result [class*=r] {
background: red;
}
.i16 {
display: inline-block;
width: 2ch;
}
.result span {
display: inline-block;
width: 4ch;
text-align: center;
}
.result [class*=g] {
background: lime;
color: black;
}
.result [class*=b] {
background: blue;
}
@media screen and (min-width:50em) and (min-height:50em) {
:root {
--size: 35em;
}
}
@media screen and (min-width:50em) and (max-height:50em) {
:root {
--size: 60vh;
}
}
const sliders = document.querySelectorAll("input[type=range]");
const colorCircles = {
r: document.getElementById("red"),
g: document.getElementById("green"),
b: document.getElementById("blue")
};
const colorValue = {
r: document.querySelectorAll(".value-r"),
g: document.querySelectorAll(".value-g"),
b: document.querySelectorAll(".value-b")
};
const colorValueHex = {
r: document.querySelectorAll(".value-rhex"),
g: document.querySelectorAll(".value-ghex"),
b: document.querySelectorAll(".value-bhex")
};
const colorValueHex16 = {
r: document.getElementById("value-rhex-16"),
g: document.getElementById("value-ghex-16"),
b: document.getElementById("value-bhex-16")
};
const colorValueHex1 = {
r: document.getElementById("value-rhex-1"),
g: document.getElementById("value-ghex-1"),
b: document.getElementById("value-bhex-1")
};
const updateColor = ( element ) => {
const id = element.attributes["data-target"].value;
const v = element.value;
colorCircles[id].style.backgroundColor = `rgb(${v*(id === 'r')} ${v*(id === 'g')} ${v*(id === 'b')})`;
console.log(colorValue[id]);
colorValue[id].forEach( (el) => {
el.innerHTML = v;
});
const hexString = parseInt(v).toString(16);
colorValueHex[id].forEach((el) => {
el.innerHTML = hexString.length === 1 ? '0'+hexString : hexString;
});
colorValueHex16[id].innerHTML = parseInt(v/16);
colorValueHex1[id].innerHTML = parseInt(v%16)
}
sliders.forEach( element => {
element.addEventListener( 'input', () => {
updateColor( element );
});
});
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.