Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Color Picker</title>
- <style>
- .section {
- margin-top: .5rem;
- border-radius: 5px;
- width: 25ex;
- background-color: #ddd;
- }
- .section div {
- clear: both;
- padding: .3em .3em .3em 0;
- }
- .picker {
- padding-bottom: .1em;
- }
- input {
- box-sizing: border-box;
- width: 17ex;
- text-align: right;
- }
- #picker {
- padding: 0;
- }
- .section ~ .section {
- display: inline-block;
- margin-right: .5ex;
- }
- .section .header {
- border-radius: 5px;
- height: 3.2em;
- background-color: black;
- color: white;
- font-size: bold;
- }
- .section span {
- vertical-align: middle;
- padding-left: .5ex;
- }
- .section input {
- float: right;
- }
- .header > input {
- margin-left: .5ex;
- }
- .section .header~div input {
- width: 7ex;
- }
- #rgbh {
- clear: both;
- margin-top: .3rem;
- width: 9ex;
- }
- </style>
- <script>
- var vRed, vGreen, vBlue, vHue, vSaturation, vLightness;
- function rgbToHSL(r, g, b) {
- r = (vRed === 255) ? 1 : (vRed / 255);
- g = (vGreen === 255) ? 1 : (vGreen / 255);
- b = (vBlue === 255) ? 1 : (vBlue / 255);
- var max = Math.max(r, g, b);
- var min = Math.min(r, g, b);
- var h, s, l = (max + min) / 2;
- if (max === min) {
- h = s = 0;
- } else {
- var d = max - min;
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
- switch (max) {
- case r:
- h = (g - b) / d + (g < b ? 6 : 0);
- break;
- case g:
- h = (b - r) / d + 2;
- break;
- case b:
- h = (r - g) / d + 4;
- break;
- }
- h /= 6;
- }
- vHue = Math.round(h * 360);
- vSaturation = Math.round(s * 240);
- vLightness = Math.round(l * 240);
- }
- function hueToRGB(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
- }
- function hslToRGB(h, s, l, r, g, b) {
- h = (vHue % 360 + 360) % 360;
- h = (h === 360) ? 1 : (h / 360);
- s = (vSaturation === 240) ? 1 : (vSaturation / 240);
- l = (vLightness === 240) ? 1 : (vLightness / 240);
- if (s === 0) {
- r = g = b = l;
- } else {
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
- var p = 2 * l - q;
- r = hueToRGB(p, q, h + 1 / 3);
- g = hueToRGB(p, q, h);
- b = hueToRGB(p, q, h - 1 / 3);
- }
- vRed = Math.round(r * 255);
- vGreen = Math.round(g * 255);
- vBlue = Math.round(b * 255);
- }
- function updateAll(e, s) {
- s = "#" +
- ("0" + vRed.toString(16).toUpperCase()).slice(-2) +
- ("0" + vGreen.toString(16).toUpperCase()).slice(-2) +
- ("0" + vBlue.toString(16).toUpperCase()).slice(-2);
- if (e !== picker) picker.value = s;
- if (e !== rgb) {
- rgb.value = `rgb(${vRed}, ${vGreen}, ${vBlue})`;
- rgb.style.backgroundColor = "";
- }
- if (e !== rgbh) {
- rgbh.value = s;
- rgbh.style.backgroundColor = "";
- }
- if (e !== red) {
- red.value = vRed;
- red.style.backgroundColor = "";
- }
- if (e !== green) {
- green.value = vGreen;
- green.style.backgroundColor = "";
- }
- if (e !== blue) {
- blue.value = vBlue;
- blue.style.backgroundColor = "";
- }
- if (e !== hsl) {
- hsl.value = `hsl(${vHue}, ${vSaturation}, ${vLightness})`;
- hsl.style.backgroundColor = "";
- }
- if (e !== hue) {
- hue.value = vHue;
- hue.style.backgroundColor = "";
- }
- if (e !== saturation) {
- saturation.value = vSaturation;
- saturation.style.backgroundColor = "";
- }
- if (e !== lightness) {
- lightness.value = vLightness;
- lightness.style.backgroundColor = "";
- }
- }
- function colorPicked(s) {
- s = picker.value.toUpperCase();
- vRed = parseInt(s.slice(1, 3), 16),
- vGreen = parseInt(s.slice(3, 5), 16),
- vBlue = parseInt(s.slice(5, 7), 16);
- rgbToHSL(vRed, vGreen, vBlue);
- updateAll();
- }
- function rgbChanged() {
- var m = this.value.match(/^rgb\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)$/i), r, g, b;
- if (m) {
- r = parseInt(m[1]);
- g = parseInt(m[2]);
- b = parseInt(m[3]);
- if ((r < 256) && (g < 256) && (b < 256)) {
- vRed = r;
- vGreen = g;
- vBlue = b;
- rgbToHSL(vRed, vGreen, vBlue);
- updateAll(this);
- this.style.backgroundColor = "";
- } else {
- this.style.backgroundColor = "pink";
- }
- } else {
- this.style.backgroundColor = "pink";
- }
- }
- function rgbhChanged() {
- var m = this.value.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i), r, g, b;
- if (m) {
- vRed = parseInt(m[1], 16);
- vGreen = parseInt(m[2], 16);
- vBlue = parseInt(m[3], 16);
- rgbToHSL();
- updateAll(this);
- this.style.backgroundColor = "";
- } else {
- this.style.backgroundColor = "pink";
- }
- }
- function componentChanged(n) {
- n = parseInt(this.value);
- if (!isNaN(n) && (n >= this.min) && (n <= this.max)) {
- switch (this) {
- case red: vRed = n; rgbToHSL(); break;
- case green: vGreen = n; rgbToHSL(); break;
- case blue: vBlue = n; rgbToHSL(); break;
- case hue: vHue = n; hslToRGB(); break;
- case saturation: vSaturation = n; hslToRGB(); break;
- case lightness: vLightness = n; hslToRGB(); break;
- }
- updateAll(this);
- this.style.backgroundColor = "";
- } else {
- this.style.backgroundColor = "pink";
- }
- }
- function hslChanged() {
- var m = this.value.match(/^hsl\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)$/i), h, s, l;
- if (m) {
- h = parseInt(m[1]);
- s = parseInt(m[2]);
- l = parseInt(m[3]);
- if ((h <= 360) && (h <= 240) && (l <= 240)) {
- vHue = h;
- vSaturation = s;
- vLightness = l;
- hslToRGB();
- updateAll(this);
- this.style.backgroundColor = "";
- } else {
- this.style.backgroundColor = "pink";
- }
- } else {
- this.style.backgroundColor = "pink";
- }
- }
- onload = function() {
- picker.onchange = colorPicked;
- rgb.oninput = rgbChanged;
- rgbh.oninput = rgbhChanged;
- red.oninput = componentChanged;
- green.oninput = componentChanged;
- blue.oninput = componentChanged;
- hsl.oninput = hslChanged;
- hue.oninput = componentChanged;
- saturation.oninput = componentChanged;
- lightness.oninput = componentChanged;
- colorPicked();
- };
- </script>
- </head>
- <body>
- <div class="section picker">
- <div>
- <label for="picker"><input value="#abcdef" id="picker" type="color"/><span>Color:</span></label>
- </div>
- </div>
- <div class="section rgb">
- <div class="header">
- <label for="rgb"><input id="rgb" /><span>RGB</span></label>
- <input id="rgbh" />
- </div>
- <div>
- <label for="red"><input id="red" type="number" min="0" max="255" /><span>Red:</span></label>
- </div>
- <div>
- <label for="green"><input id="green" type="number" min="0" max="255" /><span>Green:</span></label>
- </div>
- <div>
- <label for="blue"><input id="blue" type="number" min="0" max="255" /><span>Blue:</span></label>
- </div>
- </div>
- <div class="section hsl">
- <div class="header">
- <label for="hsl"><input id="hsl" /><span>HSL</span></label>
- </div>
- <div>
- <label for="hue"><input id="hue" type="number" min="0" max="360" /><span>Hue:</span></label>
- </div>
- <div>
- <label for="saturation"><input id="saturation" type="number" min="0" max="240" /><span>Saturation:</span></label>
- </div>
- <div>
- <label for="lightness"><input id="lightness" type="number" min="0" max="240" /><span>Lightness:</span></label>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement