Skip to content

Instantly share code, notes, and snippets.

View sidewayss's full-sized avatar

Sideways S. sidewayss

View GitHub Profile
@Jakobeha
Jakobeha / w3-color-conversions.ts
Created March 3, 2022 01:26
W3 color conversions in TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-loss-of-precision */
// Source: https://drafts.csswg.org/css-color-4/conversions.js
// Sample code for color conversions
// Conversion can also be done using ICC profiles and a Color Management System
// For clarity, a library is used for matrix multiplication (multiply-matrices.js)
export type Color = [number, number, number]
@avisek
avisek / colorConversions.js
Created May 21, 2022 12:11
RGB, HSL, XYZ, LAB, LCH color conversion algorithms in JavaScript
export function rgbToHsl(rgb) {
const r = rgb.r / 255
const g = rgb.g / 255
const b = rgb.b / 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
let h, s, l = (max + min) / 2
if (max == min) {
h = s = 0 // achromatic