Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main :class="darkMode == 'dark' ? 'dark' : ''">
	<div id="app" :style="{ boxShadow, borderColor }">

		<div id="swatch-container">
			<div id="swatch" :style="{ backgroundColor, borderColor }"></div>
			<form id="dark-mode-toggle">
				<input type="radio" v-model="darkMode" id="light" name="dark-mode" value="light">
				<label for="light"><span class="sr">Light mode</span></label>
				<input type="radio" v-model="darkMode" id="dark" name="dark-mode" value="dark">
				<label for="dark"><span class="sr">Dark mode</span></label>
			</form>
		</div>

		<form id="app-form" action="" :style="{ borderColor }">
			<h1>Automatic RGB/Hex color converter</h1>

			<div id="value-wrap" class="group">
				<label for="value">Enter a color:</label>
				<div>
					<input v-model="value" @input="validate" id="value" type="text" placeholder="Any RGB or hex value" />
				</div>
			</div>

			<div class="group">
				<ul>
					<li><strong>Format detected:</strong> {{ fromFormat }}</li>
					<li><strong>Converting to:</strong> {{ toFormat }}</li>
				</ul>
			</div>

			<div class="flex group">
				<label for="result">Converted value:</label>
			</div>

			<div class="flex">
				<input ref="output" id="result" tabindex="-1" type="text" :value="convertedValue" />
				<button @click.prevent="copy" :disabled="!convertedValue" type="button"
					ref="copyButton">{{ copyButtonText }}</button>
			</div>
		</form>
	</div>
</main>
              
            
!

CSS

              
                $gray: #333;
$cardBG: #f8f8e8;
$bg: #ddd;
$orange: #dd8800;
$size: 8;

*, *:before, *:after {
	box-sizing: inherit;
	color: $gray;
	font-size: inherit;
	background: transparent;
	transition: color .3s, background .3s;
	font-family: inherit;
}

*:focus, input[type="radio"]:focus + label {
	outline: 2px dashed $orange;
	outline-offset: 2px;
}

.sr {
	position: absolute;
	clip: rect(1px, 1px, 1px, 1px);
	width: 1px;
	height: 1px;
	left: -100vh;
	opacity: 0;
}

body {
	margin: 0;
}

main {
	display: grid;
	place-items: center;
	min-height: 100vh;
	font-family: 'Spartan';
	font-size: 18px;
	box-sizing: border-box;
	color: #333;
	background: $bg;
	text-align: left;
}

#app {
	width: 100%;
	max-width: 26rem;
	padding: 4rem 1rem;
	border: 2px solid;
	box-shadow: .5rem .5rem 0 rgba(0,0,0,.25);
	position: relative;
	background: $cardBG;
	margin: 3rem auto 0;
		
	h1 {
		font-size: 1.8rem;
	}
	
	#swatch-container {
		
		#dark-mode-toggle {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: .5rem;
			position: relative;
			z-index: 2;
			background: transparent;

			input[type="radio"] {
				@extend .sr;

				& + label {
					width: 1.25rem;
					height: 1.25rem;
					display: block;
					border-radius: 1.25rem;
					border: 2px solid;
					background: $cardBG;
					position: relative;
					
					&[for="dark"] {
						background: $gray;
					}
					
					&:not(:last-child) {
						margin-right: .5rem;
					}
				}
				
				&:checked + label:after {
					content: '';
					border-radius: 2rem;
					width: calc(1.5rem + 4px);
					height: calc(1.5rem + 4px);
					border: 2px solid;
					display: block;
					background: transparent;
					left: -6px;
					top: -6px;
					position: absolute;
				}
			}
		}
	}

	#swatch {
		width: #{$size}rem;
		height: #{$size}rem;
		border-radius: #{$size}rem;
		border: 2px solid;
		position: absolute;
		top: #{$size * -0.5}rem;
		left: calc(50% - #{$size * .5}rem);
		background-color: $cardBG;
	}
	
	ul {
		padding: 0;
		list-style-type: none;
		
		li + li {
			margin-top: .5rem;
		}
	}

	#app-form {
		margin: auto;

		input, button {
			padding: .5em;
			border-radius: .25em;
			border: 2px solid;
			font-size: 1rem;
		}
		
		input, button {
			font-family: 'Fira Code', monospace;
		}
		
		button {
			padding: .5em 1em;
			font-weight: bold;
			
			&:disabled {
				opacity: .4;
			}
		}

		label, input, .label {
			display: block;
			width: 100%;
		}
		
		input {
			padding: .75em;
		}

		label, .label {
			margin: .25em 0;
			font-weight: bold;
		}

		#result {
			border: none;
			border-radius: 0;
			padding-left: 0;
			border-bottom: 2px solid;
		}

		.flex {
			display: flex;
			align-items: stretch;
			flex-wrap: wrap;
			
			@media(min-width: 440px) {
				flex-wrap: nowrap;
				
				& > *:not(:last-child) {
					margin-right: 1rem;
				}
			}
		}

		.group {
			margin-top: 3rem;
			text-align: left;
		}

		.method {
			font-weight: bold;
		}
	}
	
	//#app
	@media (min-width: 440px) {
		padding: 4rem 3rem;	
	}
	
	@media (min-width: 768px) {
		display: grid;
		grid-template-columns: auto 1fr;
		grid-gap: 3rem;
		max-width: 768px;
		align-items: center;
		text-align: left;
		margin: 0 auto;
		
		#swatch {
			$size: 12;
			position: relative;
			left: unset;
			top: unset;
			width: #{$size}rem;
			height: #{$size}rem;
			border-radius: #{$size}rem;
		}
		
		#app-form {
			padding: 2rem 0 2rem 3rem;
			border-left: 2px dashed;
		}
	}
}

.fade-enter-active, .fade-leave-active {
	transition: opacity .8s;
}

.fade-enter, .fade-leave-to {
	opacity: 0;
	transform: translateY(2rem)
}

#dark-mode-switch {
	position: absolute;
	z-index: 3;
	width: 3rem;
	height: 2rem;
	border: 2px solid;
	left: .5rem;
	top: .5rem;
	border-radius: 3rem;
}






main.dark {
	color: #fff;
	$gray: #fff;
	$cardBG: #333;
	$bg: #222;
	$orange: #dd8800;
	background: $bg;
		
	* {
		color: $gray;
	}

	#app {
		background: $cardBG;
		box-shadow: .5rem .5rem 0 rgba(0,0,0,.7);
	
		#swatch-container {

			#dark-mode-toggle {

				input[type="radio"] {

					&:checked + label:after {
						border: 2px solid #fff;
					}
				}
			}

			#swatch {
				background-color: $cardBG;
			}
		}
	}
}
              
            
!

JS

              
                const noMethod = { from: null, to: null };

const app = new Vue({
	data: () => ({
		base16: "0123456789abcdef",
		hexRange: [],
		value: "",
		method: noMethod,
		copied: false,
		darkMode: "light"
	}),
	created() {
		const digits = this.base16.split("");
		digits.forEach(outer => {
			digits.forEach(inner => {
				this.hexRange.push(outer + inner);
			});
		});
	},
	watch: {
		value(next, prev) {
			if (
				next.match(/^rgba/) ||
				(next.match(/,/g) && next.match(/,/g).length == 3)
			) {
				this.method = { from: "RGBA", to: "Hex + alpha" };
			} else if (["r", "R"].includes(next.substr(0, 1)) || next.includes(",")) {
				this.method = { from: "RGB", to: "Hex" };
			} else if (next.match(/^#?([a-f0-9]{4}$|[a-f0-9]{8})/i)) {
				this.method = { from: "Hex + alpha", to: "RGBA" };
			} else if (next.match(/[a-f0-9]+/i)) {
				this.method = { from: "Hex", to: "RGB" };
			} else {
				this.method = noMethod;
			}
		}
	},
	methods: {
		convert(val) {
			if (val.match(/^rgb.*/) || val.match(/.*,.*/)) {
				return this.rgbToHex(val);
			} else {
				return this.hexToRGB(val);
			}
		},

		hexToRGB(val) {
			//Remove '#' from string if present
			if (val.substr(0, 1) === "#") {
				val = val.substr(1);
			}
			//Make sure we have the right number of characters
			if ([3, 4, 6, 8].includes(val.length)) {
				//Allow for hex shorthand (3 or 4 characters)
				const division = val.length >= 6 ? 2 : 1;
				const color = {
					r: { from: val.substr(0, division) },
					g: { from: val.substr(division, division) },
					b: { from: val.substr(division * 2, division) },
					a: { from: val.substr(division * 3, division) }
				};

				//Set single-character shorthands to double-character hexes
				if (division === 1) {
					["r", "g", "b", "a"].forEach(col => {
						color[col].from += color[col].from;
					});
				}

				//Matches the hex values to their 0-255 counterparts
				this.hexRange.forEach((hex, index) => {
					Object.keys(color).forEach(key => {
						if (color[key].from.toLowerCase() == hex) {
							color[key].to = index;
						}
					});
				});

				//See if an alpha value was provided (return RGBA or RGB)
				if (color.a.to) {
					return `rgba(${color.r.to}, ${color.g.to}, ${color.b.to}, ${(
						color.a.to / 255
					).toFixed(2)})`;
				} else {
					return `rgb(${color.r.to}, ${color.g.to}, ${color.b.to})`;
				}
			} else {
				return null;
			}
		},

		rgbToHex(val) {
			//Don't try to do stuff if we don't have two commas and a number
			if (!val.match(/.*,.*,[0-9\s]+/)) return;

			//Split up the RGB(A) string on commas and match numbers
			const rgba = val.split(",");
			const vals = rgba.map(color => color && color.match(/[0-9\.]+/));
			let invalid = false;

			//Make sure none of the numbers are larger than 255
			vals.forEach(val => {
				if (val > 255) invalid = true;
			});

			//Make sure we have 3 or 4 numbers (RGB or RGBA)
			if ([3, 4].includes(vals.length)) {
				let finalHex = "";
				vals.forEach((color, colorIndex) => {
					//If we're dealing with the alpha value…
					if (colorIndex == 3)
						color = Math.ceil(color * 255 > 255 ? 255 : color * 255);
					this.hexRange.forEach((hex, hexIndex) => {
						if (color == hexIndex) finalHex += hex;
					});
				});
				return invalid ? null : `#${finalHex}`;
			}
		},

		validate(key) {
			//Only allow the characters that go into RGBA and/or hex volors
			this.value = this.value.replace(/[^#a-f0-9rg\(\),\s\.]/, "");
		},

		copy() {
			this.$refs.output.select();
			document.execCommand("copy");
			this.copied = true;
			this.$refs.copyButton.focus();
			setTimeout(() => {
				this.copied = false;
			}, 2000);
		}
	},
	computed: {
		convertedValue() {
			return this.convert(this.value);
		},
		fromFormat() {
			return this.method && this.method.from ? this.method.from : "…";
		},
		toFormat() {
			return this.method && this.method.to ? this.method.to : "…";
		},
		copyButtonText() {
			return this.copied ? "Copied!" : "Copy";
		},
		boxShadow() {
			return `.5rem .5rem 0 ${this.convert(this.value) || "rgba(0,0,0,.25)"}`;
		},
		borderColor() {
			return this.convert(this.value) || "inherit";
		},
		backgroundColor() {
			return this.convert(this.value) || "";
		}
	}
});

app.$mount(document.querySelector("main"));

              
            
!
999px

Console