Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <h1 class="text">It's Too Dark!!</h1>

<div class="container">
	<div class="colorpicker">
		<div class="color">
			<label id="red">
					R : <span class="value">0</span>
			</label>
			<input type="range"
					 style="--slider-thumb-color: #fff;"
					 id="redSlider" class="slider"
					 min="0" max="255"
					 step="1" value="0" />
		</div>
		<div class="color">
			<label id="green">
				G : <span class="value">0</span>
			</label>
			<input type="range"
					 style="--slider-thumb-color: #fff;"
					 id="greenSlider" class="slider"
					 min="0" max="255"
					 step="1" value="0" />
		</div>
		<div class="color">
			<label id="blue">
				B : <span class="value">0</span>
			</label>
			<input type="range"
					 style="--slider-thumb-color: #fff;"
					 id="blueSlider" class="slider"
					 min="0" max="255"
					 step="1" value="0" />
		</div>
	</div>
</div>


<div class="support">
	<a href="https://twitter.com/DevLoop01" target="_blank"><i class="fab fa-twitter-square"></i></a>
	<a href="https://codepen.io/dev_loop/" target="_blank"><i class="fab fa-codepen"></i></a>
</div>

              
            
!

CSS

              
                * {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}
body {
	width: 100%;
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: #000;
	font-family: "montserrat";
	overflow: hidden;
}
h1.text {
	color: #fff;
	font-size: 2.5rem;
	margin-bottom: 40px;
	transition: all 0.15s ease-in-out;
	white-space: nowrap;
}
.container {
	width: 300px;
	height: 200px;
}
.colorpicker {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	padding: 15px 30px;
	border-radius: 15px;
}
.color {
	position: relative;
	background: rgba(255, 255, 255, 0.15);
	width: 100%;
	height: 60px;
	margin: 8px 0;
	display: flex;
	flex-direction: column;
	border-radius: 15px;
	transition: all 0.15s ease-in-out;
	label {
		position: absolute;
		top: -10px;
		left: 20px;
		color: #fff;
		transition: all 0.15s ease-in-out;
	}
	input {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
}

$range-handle-size: 20px !default;

$range-track-color: rgba(255, 255, 255, 0.314) !default;
$range-track-height: 2px !default;

$range-label-width: 60px !default;

.slider {
	-webkit-appearance: none;
	width: calc(100% - (#{$range-label-width + 0px}));
	height: $range-track-height;
	border-radius: 5px;
	background: $range-track-color;
	outline: none;
	padding: 0;
	margin: 0;
	cursor: pointer;
	transition: all 0.15s ease-in-out;

	&::-webkit-slider-thumb {
		-webkit-appearance: none;
		width: $range-handle-size;
		height: $range-handle-size;
		border-radius: 50%;
		background: var(--slider-thumb-color);
		cursor: pointer;
		transition: all 0.15s ease-in-out;
		&:hover {
			transform: scale(1.1);
		}
	}
	&::-moz-range-thumb {
		width: $range-handle-size;
		height: $range-handle-size;
		border: 0;
		border-radius: 50%;
		background: var(--slider-thumb-color);
		cursor: pointer;
		transition: background 0.15s ease-in-out;
		&:hover {
			transform: scale(1.1);
		}
	}
}



.support{
	position: fixed;
	right: 10px;
	bottom: 10px;
	padding: 10px;
	display: flex;
}
a{
	margin: 0 20px;
	color: #fff;
	font-size: 2rem;
	transition: all 400ms ease;
}

a:hover{
	color: #222;
}
              
            
!

JS

              
                console.clear();

// Selecting all the icons - Twitter & Codepen
const socialIcons = Array.from(document.querySelectorAll('.fab'));

const sliderProps = {
	fill: "#fff",
	background: "rgba(255, 255, 255, 0.214)",
};

// Selecting All the Slider Containers
const sliderContainers = Array.from(document.querySelectorAll(".color"));
const displayText = document.querySelector(".text");

// This function is responsible to create the trailing color and setting the fill.
function applyFill(slider) {
	const percentage = (100 * (slider.value - slider.min)) / (slider.max - slider.min);
	const bg = `linear-gradient(90deg, ${sliderProps.fill} ${percentage}%, ${sliderProps.background} ${percentage +
			0.1}%)`;
	slider.style.background = bg;
}

// Using Event Listener to apply the fill and also change the value of the text also the bgcolor based on brightness.
sliderContainers.forEach(sliderContainer => {
	const rangeInput = sliderContainer.querySelector("input");
	const rangeValue = sliderContainer.querySelector(".value");
	rangeInput.addEventListener("input", event => {
		rangeValue.innerHTML = event.target.value;
		applyFill(event.target, rangeValue);

		let redSlider = document.getElementById("redSlider").value;
		let greenSlider = document.getElementById("greenSlider").value;
		let blueSlider = document.getElementById("blueSlider").value;

		// use ML to determine result
		let networkObject = createNetworkObject(redSlider, greenSlider, blueSlider);
		let MLresult = network.run(networkObject);

		MLresult = networkLabel(MLresult);

		// change colors
		changeBackgroundColor(redSlider, greenSlider, blueSlider);
		changeElementsColor(MLresult);
	});
});

// Neural Network part

const network = new brain.NeuralNetwork();

// train algorithm with known data
network.train([
	{ input: { r: 0.62, g: 0.72, b: 0.88 }, output: { light: 1 } },
	{ input: { r: 0.1, g: 0.84, b: 0.72 }, output: { light: 1 } },
	{ input: { r: 0.74, g: 0.78, b: 0.86 }, output: { light: 1 } },
	{ input: { r: 0.33, g: 0.24, b: 0.29 }, output: { dark: 1 } },
	{ input: { r: 0.31, g: 0.35, b: 0.41 }, output: { dark: 1 } },
	{ input: { r: 1, g: 0.42, b: 0.52 }, output: { dark: 1 } },
	{ input: { r: 0, g: 0, b: 1 }, output: { dark: 1 } },
	{ input: { r: 0.8, g: 0.44, b: 1 }, output: { dark: 1 } },
	{ input: { r: 0, g: 0.44, b: 1 }, output: { dark: 1 } },
	{ input: { r: 0.3, g: 0.6, b: 1 }, output: { dark: 1 } },
	{ input: { r: 0.1, g: 0.6, b: 0 }, output: { dark: 1 } },
]);

// creating an object for neural network
function createNetworkObject(r, g, b) {
	let networkObject = { r: null, g: null, b: null };
	networkObject.r = r / 255;
	networkObject.g = g / 255;
	networkObject.b = b / 255;

	return networkObject;
}

// Machine learning probability label - determining the result : Light OR Dark
function networkLabel(result) {
	if (result.light > result.dark) {
		result = "light";
	} else {
		result = "dark";
	}
	return result;
}
// Change the background color based on the slider value.
function changeBackgroundColor(r, g, b) {
	document.body.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
// change colors on page relative to background
function changeElementsColor(result) {
	sliderContainers.forEach(sliderContainer => {
		const slider = sliderContainer.querySelector("input");
		if (result == "light") {
			sliderProps.fill = "#000";
			sliderProps.background = "rgba(0, 0, 0, 0.214)";
			applyFill(slider);

			sliderContainer.style.background = "rgba(0, 0, 0, 0.15)";
			sliderContainer.querySelector("label").style.color = "#000";

			slider.style.setProperty("--slider-thumb-color", "#000");

			displayText.style.color = "#000";
			displayText.innerHTML = "Now It's Bright!!";

			socialIcons.forEach(icon => {
				icon.style.color = '#000'
			})
		} else if (result == "dark") {
			sliderProps.fill = "#fff";
			sliderProps.background = "rgba(255, 255, 255, 0.214)";
			applyFill(slider);

			sliderContainer.style.background = "rgba(255, 255, 255, 0.15)";
			sliderContainer.querySelector("label").style.color = "#fff";

			slider.style.setProperty("--slider-thumb-color", "#fff");

			displayText.style.color = "#fff";
			displayText.innerHTML = "It's Quite Dark!!";

			socialIcons.forEach(icon => {
				icon.style.color = '#fff'
			})
		}
	});
}

              
            
!
999px

Console