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>
	<div>
		<label for="input-image">Image</label>
		<input type="file" accept="image/*" id="input-image">
	</div>
	
	<div class="control-list">
		<div>
			<label for="input-brightness">Brightness</label>
			<input type="range" min="-1" max="1" step="0.05" id="input-brightness">
		</div>

		<div>
			<label for="input-contrast">Contrast</label>
			<input type="range" min="-1" max="1" step="0.05" id="input-contrast">
		</div>

		<div>
			<label for="input-saturation">Saturation</label>
			<input type="range" min="-1" max="1" step="0.05" id="input-saturation">
		</div>

		<div>
			<label for="input-exposure">Exposure</label>
			<input type="range" min="-1" max="1" step="0.05" id="input-exposure">
		</div>
	</div>
	
	<div id="canvas-container"></div>
</main>
              
            
!

CSS

              
                canvas {
	display: block;
	height: auto !important;
	width: 36rem !important;
	
	image-rendering: pixelated;
}

label {
	display: block;
	font-weight: bold;
	
	margin-bottom: 0.25rem;
}

main {
	padding: 1rem;
}

main > * {
	margin-bottom: 1rem;
}

.control-list {
	display: flex;
	flex-flow: row wrap;
}

.control-list > :not(:last-child) {
	margin-right: 1rem;
}
              
            
!

JS

              
                import {
	Plane,
	Program,
	Mat4,
	Mesh,
	Renderer,
	Texture,
	Vec4,
} from 'https://unpkg.com/ogl@0.0.49/src/index.mjs';

import { transpose } from 'https://unpkg.com/ogl@0.0.49/src/math/functions/Mat4Func.js';

class ColorMatrix {
	constructor() {
		this._columnCount = 5;
		this._rowCount = 4;
		this._cellCount = this._columnCount * this._rowCount;
		
		this._matrix = new Array(this._cellCount).fill(0).map((_, index) => {
			const row = Math.floor(index / this._columnCount);
			
			return index % this._columnCount === row ? 1 : 0;
		});
	}
	
	getOffsetVector() {
		return this._matrix.filter((_, index) => index % this._columnCount === 4);
	}
	
	getRgbaMatrix() {
		return this._matrix.filter((_, index) => {
			const column = index % this._columnCount;
			
			return column < 4;
		});
	}
	
	set(...args) {
		if (args.length !== this._cellCount) {
			throw new Error(`Number of values must be ${this._cellCount}`);
		}
		
		this._matrix = args;
		
		return this;
	}
}

class ImageLoader {
	static loadFromUrl(src) {
		const image = new Image();
		
		image.setAttribute('anonymous', true);
		image.setAttribute('crossorigin', true);
		
		return new Promise((resolve, reject) => {
			image.addEventListener('load', () => resolve(image));
			image.addEventListener('error', () => reject(new Error('Unable to load image')));
			image.src = src;
		});
	}
	
	static loadFromFile(file) {
		return ImageLoader.loadFromUrl(URL.createObjectURL(file));
	}
}

class Vec4Utils {
	static multiply(a, b) {
		return new Vec4(new Array(4).fill(1).map((_, index) => {
			return a[index] * b[index];
		}));
	}
}

class Application {
	constructor() {
		this._renderer = new Renderer();
		this._map = new Texture(this._gl);
		this._mesh = this.createMesh(this._map);
		
		this._brightnessMatrix = new ColorMatrix();
		this._contrastMatrix = new ColorMatrix();
		this._exposureMatrix = new ColorMatrix();
		this._saturationMatrix = new ColorMatrix();
	}
	
	get canvas() {
		return this._renderer.gl.canvas;
	}
	
	createMesh(map) {
		const geometry = new Plane(this._gl, {
			height: 2,
			width: 2,
		});
		
		const program = new Program(this._gl, {
			fragment: `
				precision mediump float;

				uniform sampler2D u_map;

				uniform mat4 u_brightnessMatrix;
				uniform vec4 u_brightnessOffset;
				uniform mat4 u_contrastMatrix;
				uniform vec4 u_contrastOffset;
				uniform mat4 u_exposureMatrix;
				uniform vec4 u_exposureOffset;
				uniform mat4 u_saturationMatrix;
				uniform vec4 u_saturationOffset;

				varying vec2 v_uv;

				void main() {
					vec4 texel = texture2D(u_map, v_uv);
					mat4 matrix = u_brightnessMatrix * u_contrastMatrix * u_exposureMatrix * u_saturationMatrix;
					vec4 offset = u_brightnessOffset + u_contrastOffset + u_exposureOffset + u_saturationOffset;

					gl_FragColor = matrix * texel + offset;
				}
			`,
			vertex: `
				attribute vec4 position;
				attribute vec2 uv;

				varying vec2 v_uv;

				void main() {
					v_uv = uv;

					gl_Position = position;
				}
			`,
			
			uniforms: {
				u_brightnessMatrix: { value: new Mat4() },
				u_brightnessOffset: { value: new Vec4() },
				u_contrastMatrix: { value: new Mat4() },
				u_contrastOffset: { value: new Vec4() },
				u_exposureMatrix: { value: new Mat4() },
				u_exposureOffset: { value: new Vec4() },
				u_saturationMatrix: { value: new Mat4() },
				u_saturationOffset: { value: new Vec4() },
				u_map: { value: map },
			},
		});
		
		return new Mesh(this._gl, {
			geometry,
			program,
		});
	}
	
	setBrightness(brightness) {
		this._mesh.program.uniforms.u_brightnessOffset.value.set(
			brightness,
			brightness,
			brightness,
			0,
		);
		
		this.render();
	}
	
	setContrast(contrast) {
		const c = 1 + contrast;
		const o = 0.5 * (1 - c);
		
		transpose(this._mesh.program.uniforms.u_contrastMatrix.value, [
			c, 0, 0, 0,
			0, c, 0, 0,
			0, 0, c, 0,
			0, 0, 0, 1,
		]);
		
		this._mesh.program.uniforms.u_contrastOffset.value.set(o, o, o, 0);
		
		this.render();
	}
	
	setExposure(exposure) {
		const e = 1 + exposure;
		
		transpose(this._mesh.program.uniforms.u_exposureMatrix.value, [
			e, 0, 0, 0,
			0, e, 0, 0,
			0, 0, e, 0,
			0, 0, 0, 1,
		]);
		
		this.render();
	}
	
	setImage(image) {
		this._map.image = image;
		this._map.needsUpdate = true;
		
		this.setSize(image.naturalWidth, image.naturalHeight);
		this.render();
	}
	
	setSaturation(saturation) {
		const s = 1 + saturation;
		
		// https://www.w3.org/TR/WCAG20/#relativeluminancedef
		const lr = 0.2126;
		const lg = 0.7152;
		const lb = 0.0722;
		
		const sr = (1 - s) * lr;
		const sg = (1 - s) * lg;
		const sb = (1 - s) * lb;
		
		transpose(this._mesh.program.uniforms.u_saturationMatrix.value, [
			sr + s, sg    , sb    , 0,
			sr    , sg + s, sb    , 0,
			sr    , sg    , sb + s, 0,
			0     , 0     , 0     , 1,
		]);
		
		this.render();
	}
	
	setSize(width, height) {
		this._renderer.setSize(width, height);
	}
	
	render() {
		this._renderer.render({
			scene: this._mesh,
		});
	}
	
	get _gl() {
		return this._renderer.gl;
	}
}

(async () => {
	const inputBrightness = document.getElementById('input-brightness');
	const inputContrast = document.getElementById('input-contrast');
	const inputExposure = document.getElementById('input-exposure');
	const inputImage = document.getElementById('input-image');
	const inputSaturation = document.getElementById('input-saturation');
	
	const image = await ImageLoader.loadFromUrl('https://images.unsplash.com/photo-1525253013412-55c1a69a5738?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1024&q=90');
	const app = new Application();
	
	app.setImage(image);
	app.render();

	document.getElementById('canvas-container').appendChild(app.canvas);
	
	inputBrightness.addEventListener('input', () => app
		.setBrightness(Number.parseFloat(inputBrightness.value)));
	inputContrast.addEventListener('input', () => app
		.setContrast(Number.parseFloat(inputContrast.value)));
	inputExposure.addEventListener('input', () => app
		.setExposure(Number.parseFloat(inputExposure.value)));
	inputSaturation.addEventListener('input', () => app
		.setSaturation(Number.parseFloat(inputSaturation.value)));

	inputImage.addEventListener('change', async (event) => {
		if (event.target.files.length > 0) {
			const image = await ImageLoader.loadFromFile(event.target.files[0]);

			app.setImage(image);
		}
	});
})();
              
            
!
999px

Console