CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 16:04:12 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"018432af4ef037ca736d7780a4b11c5f"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=BLZ8LGMCFXOHsT5Kyy1672%2B%2FddJ%2BLd6lOTkJywl9R24M1NaJDGhP%2BnD9ZtA0aCK2rQeakoFx03VhU3rZXuZhsZgXK1tYqGnSXQMFpqHvPNZjtfxTN59uyLlAy6I0stL1F%2BL4Ju%2Fx1VFGgsCKdd39n7QnbqT9rZhoh2hihjWpEJKB8KLcrWmJIujSPpc%2BInZzvVcDUNukFlM2%2B5l2QD59K0EzJP0WvWxYq4tySNvkAyAHJM1piUDzyXdXGYUyrhFraxrFQJ1i1M5hmmR0wvhHSQ%3D%3D--xMEfQx%2FduVttomWj--hcasxRohHK1E5vUTFrbuEA%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.197782239.1760198652; Path=/; Domain=github.com; Expires=Sun, 11 Oct 2026 16:04:12 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Sun, 11 Oct 2026 16:04:12 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 8DB0:2F198C:215049:29FC97:68EA7FFC
RGB, HSL, XYZ, LAB, LCH color conversion algorithms in JavaScript · GitHub
Show Gist options
Save avisek/eadfbe7a7a169b1001a2d3affc21052e to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
July 8, 2025 07:27
-
Star
12
(12)
You must be signed in to star a gist -
Fork
2
(2)
You must be signed in to fork a gist
-
Save avisek/eadfbe7a7a169b1001a2d3affc21052e to your computer and use it in GitHub Desktop.
RGB, HSL, XYZ, LAB, LCH color conversion algorithms in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Color conversion algorithms (RGB, HSL, XYZ, LAB, LCH) | |
* Derived from jQuery Color Picker Sliders by István Ujj-Mészáros | |
* Licensed under the Apache License 2.0 | |
* https://github.com/istvan-ujjmeszaros/jquery-colorpickersliders | |
*/ | |
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 | |
} else { | |
const 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 | |
} | |
h *= 360 | |
s *= 100 | |
l *= 100 | |
return { h, s, l } | |
} | |
export function hslToRgb(hsl) { | |
const h = hsl.h / 360 | |
const s = hsl.s / 100 | |
const l = hsl.l / 100 | |
let r, g, b | |
if (s == 0) { | |
r = g = b = l // achromatic | |
} else { | |
function hue2rgb(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 | |
} | |
const q = l < 0.5 ? l * (1 + s) : l + s - l * s | |
const p = 2 * l - q | |
r = hue2rgb(p, q, h + 1/3) | |
g = hue2rgb(p, q, h) | |
b = hue2rgb(p, q, h - 1/3) | |
} | |
r *= 255 | |
g *= 255 | |
b *= 255 | |
return { r, g, b} | |
} | |
export function rgbToXyz(rgb) { | |
let r = rgb.r / 255 | |
let g = rgb.g / 255 | |
let b = rgb.b / 255 | |
if (r > 0.04045) { | |
r = Math.pow(((r + 0.055) / 1.055), 2.4) | |
} else { | |
r = r / 12.92 | |
} | |
if (g > 0.04045) { | |
g = Math.pow(((g + 0.055) / 1.055), 2.4) | |
} else { | |
g = g / 12.92 | |
} | |
if (b > 0.04045) { | |
b = Math.pow(((b + 0.055) / 1.055), 2.4) | |
} else { | |
b = b / 12.92 | |
} | |
r *= 100 | |
g *= 100 | |
b *= 100 | |
// Observer = 2°, Illuminant = D65 | |
const x = r * 0.4124 + g * 0.3576 + b * 0.1805 | |
const y = r * 0.2126 + g * 0.7152 + b * 0.0722 | |
const z = r * 0.0193 + g * 0.1192 + b * 0.9505 | |
return { x, y, z} | |
} | |
export function xyzToLab(xyz) { | |
// Observer = 2°, Illuminant = D65 | |
let x = xyz.x / 95.047 | |
let y = xyz.y / 100.000 | |
let z = xyz.z / 108.883 | |
if (x > 0.008856) { | |
x = Math.pow(x, 0.333333333) | |
} else { | |
x = 7.787 * x + 0.137931034 | |
} | |
if (y > 0.008856) { | |
y = Math.pow(y, 0.333333333) | |
} else { | |
y = 7.787 * y + 0.137931034 | |
} | |
if (z > 0.008856) { | |
z = Math.pow(z, 0.333333333) | |
} else { | |
z = 7.787 * z + 0.137931034 | |
} | |
const l = (116 * y) - 16 | |
const a = 500 * (x - y) | |
const b = 200 * (y - z) | |
return { l, a, b } | |
} | |
export function labToLch(lab) { | |
const { l, a, b } = lab | |
const c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) | |
let h = Math.atan2(b, a) //Quadrant by signs | |
if (h > 0) { | |
h = (h / Math.PI) * 180 | |
} else { | |
h = 360 - (Math.abs(h) / Math.PI) * 180 | |
} | |
return { l, c, h } | |
} | |
export function lchToLab(lch) { | |
const { l, c, h } = lch | |
const a = Math.cos(h * 0.01745329251) * c | |
const b = Math.sin(h * 0.01745329251) * c | |
return { l, a, b } | |
} | |
export function labToXyz(lab) { | |
const { l, a, b } = lab | |
let y = (l + 16) / 116 | |
let x = a / 500 + y | |
let z = y - b / 200 | |
if (Math.pow(y, 3) > 0.008856) { | |
y = Math.pow(y, 3) | |
} else { | |
y = (y - 0.137931034) / 7.787 | |
} | |
if (Math.pow(x, 3) > 0.008856) { | |
x = Math.pow(x, 3) | |
} else { | |
x = (x - 0.137931034) / 7.787 | |
} | |
if (Math.pow(z, 3) > 0.008856) { | |
z = Math.pow(z, 3) | |
} else { | |
z = (z - 0.137931034) / 7.787 | |
} | |
// Observer = 2°, Illuminant = D65 | |
x = 95.047 * x | |
y = 100.000 * y | |
z = 108.883 * z | |
return { x, y, z } | |
} | |
export function xyzToRgb(xyz) { | |
// Observer = 2°, Illuminant = D65 | |
const x = xyz.x / 100 // X from 0 to 95.047 | |
const y = xyz.y / 100 // Y from 0 to 100.000 | |
const z = xyz.z / 100 // Z from 0 to 108.883 | |
let r = x * 3.2406 + y * -1.5372 + z * -0.4986 | |
let g = x * -0.9689 + y * 1.8758 + z * 0.0415 | |
let b = x * 0.0557 + y * -0.2040 + z * 1.0570 | |
if (r > 0.0031308) { | |
r = 1.055 * (Math.pow(r, 0.41666667)) - 0.055 | |
} else { | |
r = 12.92 * r | |
} | |
if (g > 0.0031308) { | |
g = 1.055 * (Math.pow(g, 0.41666667)) - 0.055 | |
} else { | |
g = 12.92 * g | |
} | |
if (b > 0.0031308) { | |
b = 1.055 * (Math.pow(b, 0.41666667)) - 0.055 | |
} else { | |
b = 12.92 * b | |
} | |
r *= 255 | |
g *= 255 | |
b *= 255 | |
return { r, g, b } | |
} | |
// const rgb = { r: 64, g: 128, b: 192 } | |
// console.log(rgb) | |
// console.log(rgbToHsl(rgb)) | |
// console.log(hslToRgb(rgbToHsl(rgb))) | |
// console.log(rgb) | |
// console.log(rgbToXyz(rgb)) | |
// console.log(xyzToLab(rgbToXyz(rgb))) | |
// console.log(labToLch(xyzToLab(rgbToXyz(rgb)))) | |
// console.log(lchToLab(labToLch(xyzToLab(rgbToXyz(rgb))))) | |
// console.log(labToXyz(lchToLab(labToLch(xyzToLab(rgbToXyz(rgb)))))) | |
// console.log(xyzToRgb(labToXyz(lchToLab(labToLch(xyzToLab(rgbToXyz(rgb))))))) |
@vezwork Thanks for asking! This code is derived from a library (jQuery Color Picker Sliders), which is licensed under Apache License 2.0. I've just updated the gist with proper attribution - feel free to use it under those terms.
Awesome! Thanks much @avisek
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.
Hi, what is the license on this gist? I'd like to use the code but don't want to disrespect your copyright