-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix(colors): fix colors conversion
- Loading branch information
1 parent
e363659
commit c09dca8
Showing
9 changed files
with
116 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,17 @@ | ||
/* eslint-disable no-param-reassign */ | ||
/* eslint-disable no-bitwise */ | ||
import { useAppConfiguration } from "@/frontend/hooks/configuration/configuration.store"; | ||
|
||
import { hexToOklch } from "../lib/colors/conversion"; | ||
import { usePortalThemes } from "./portal"; | ||
|
||
type ThreeNumbers = readonly [number, number, number]; | ||
|
||
function hexToRgb(hex$1: string): ThreeNumbers { | ||
let hex = hex$1.replace("#", ""); | ||
if (hex.length === 3) { | ||
hex = hex | ||
.split("") | ||
.map((c) => c + c) | ||
.join(""); | ||
} | ||
const bigint = parseInt(hex, 16); | ||
const r = (bigint >> 16) & 255; | ||
const g = (bigint >> 8) & 255; | ||
const b = bigint & 255; | ||
return [r, g, b]; | ||
} | ||
|
||
function rgbToXyz([r, g, b]: ThreeNumbers) { | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
|
||
r = r > 0.04045 ? ((r + 0.055) / 1.055) ** 2.4 : r / 12.92; | ||
g = g > 0.04045 ? ((g + 0.055) / 1.055) ** 2.4 : g / 12.92; | ||
b = b > 0.04045 ? ((b + 0.055) / 1.055) ** 2.4 : b / 12.92; | ||
|
||
const x = (r * 0.4124564 + g * 0.3575761 + b * 0.1804375) / 0.95047; | ||
const y = (r * 0.2126729 + g * 0.7151522 + b * 0.072175) / 1.0; | ||
const z = (r * 0.0193339 + g * 0.119192 + b * 0.9503041) / 1.08883; | ||
|
||
return [x, y, z] as const; | ||
} | ||
|
||
function xyzToLab([x, y, z]: ThreeNumbers) { | ||
x = x > 0.008856 ? Math.cbrt(x) : 7.787 * x + 16 / 116; | ||
y = y > 0.008856 ? Math.cbrt(y) : 7.787 * y + 16 / 116; | ||
z = z > 0.008856 ? Math.cbrt(z) : 7.787 * z + 16 / 116; | ||
|
||
const l = 116 * y - 16; | ||
const a = 500 * (x - y); | ||
const b = 200 * (y - z); | ||
|
||
return [l, a, b] as const; | ||
} | ||
|
||
function labToOklch([l, a, b]: ThreeNumbers) { | ||
const c = Math.sqrt(a * a + b * b); | ||
const h = Math.atan2(b, a); | ||
const hDegrees = (h * 180) / Math.PI; | ||
const hPositive = hDegrees < 0 ? hDegrees + 360 : hDegrees; | ||
|
||
return [l / 100, c / 100, hPositive] as const; | ||
} | ||
|
||
function hexToOklch(hex: string) { | ||
const rgb = hexToRgb(hex); | ||
const xyz = rgbToXyz(rgb); | ||
const lab = xyzToLab(xyz); | ||
const [l, c, h] = labToOklch(lab); | ||
return `${l.toFixed(3)}% ${c.toFixed(3)} ${h.toFixed(3)}`; | ||
} | ||
|
||
export const useAppTheme = () => { | ||
const themeColor = useAppConfiguration("theme_color"); | ||
|
||
usePortalThemes(); | ||
|
||
const { l, c, h } = hexToOklch(themeColor.data.primary); | ||
|
||
document.documentElement.style.setProperty( | ||
"--app-primary", | ||
hexToOklch(themeColor.data.primary) | ||
`${l}% ${c} ${h}` | ||
); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ReactNode } from "react"; | ||
import { useEffect } from "react"; | ||
|
||
interface IProps { | ||
children: ReactNode; | ||
isOn: boolean; | ||
} | ||
|
||
const gaussianPortals = ["gaussian-portal-0", "gaussian-portal-1"]; | ||
|
||
export function AppBlur({ children, isOn }: IProps): JSX.Element { | ||
useEffect(() => { | ||
if (isOn) { | ||
gaussianPortals.forEach((portal) => { | ||
document.getElementById(portal)?.classList.add("gaussian-blur"); | ||
}); | ||
setTimeout(() => { | ||
document.body.style.pointerEvents = "auto"; | ||
}, 500); | ||
} else { | ||
gaussianPortals.forEach((portal) => { | ||
document.getElementById(portal)?.classList.remove("gaussian-blur"); | ||
}); | ||
} | ||
}, [isOn]); | ||
|
||
return children as JSX.Element; | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
type TRGB = { r: number; g: number; b: number }; | ||
type TLCH = { l: number; c: number; h: number }; | ||
|
||
export const hexToOklch = (hex: string) => { | ||
const hexToRGB = (h: string): TRGB => { | ||
const r: number = parseInt(h.slice(1, 3), 16); | ||
const g: number = parseInt(h.slice(3, 5), 16); | ||
const b: number = parseInt(h.slice(5, 7), 16); | ||
return { r, g, b }; | ||
}; | ||
|
||
const rgbToOklch = (rgb: TRGB): TLCH => { | ||
const r = rgb.r / 255; | ||
const g = rgb.g / 255; | ||
const b = rgb.b / 255; | ||
|
||
const [linearR, linearG, linearB] = [r, g, b].map((c: number) => | ||
c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4 | ||
); | ||
|
||
// Convert Linear RGB to CIE XYZ | ||
let x: number = | ||
linearR * 0.4124564 + linearG * 0.3575761 + linearB * 0.1804375; | ||
let y: number = | ||
linearR * 0.2126729 + linearG * 0.7151522 + linearB * 0.072175; | ||
let z: number = | ||
linearR * 0.0193339 + linearG * 0.119192 + linearB * 0.9503041; | ||
|
||
// Convert CIE XYZ to CIELAB | ||
[x, y, z] = [x, y, z].map((c: number) => | ||
c > 0.008856 ? c ** (1 / 3) : (903.3 * c + 16) / 116 | ||
); | ||
let l: number = 116 * y - 16; | ||
const a: number = 500 * (x - y); | ||
const bStar: number = 200 * (y - z); | ||
|
||
// Convert CIELAB to Oklch | ||
// let c: number = Math.sqrt(a * a + bStar * bStar); | ||
let h: number = Math.atan2(bStar, a) * (180 / Math.PI); | ||
if (h < 0) h += 360; | ||
|
||
// Assume c_max is the maximum chroma value observed or expected in your conversions | ||
const c_max = 100; /* your determined or observed maximum chroma value */ | ||
// Adjusted part of the rgbToOklch function for calculating 'c' | ||
let c: number = Math.sqrt(a * a + bStar * bStar); | ||
c = (c / c_max) * 0.37; // Scale c to be within 0 and 0.37 | ||
|
||
// Scale and round values to match the specified ranges | ||
l = Math.round(((l + 16) / 116) * 1000) / 10; // Scale l to be between 0 and 1 | ||
c = Number(c.toFixed(2)); // Ensure c is correctly scaled, adjust if necessary based on your color space calculations | ||
h = Number(h.toFixed(1)); // h is already within 0 to 360 | ||
|
||
return { | ||
l, | ||
c, | ||
h, | ||
}; | ||
}; | ||
|
||
return rgbToOklch(hexToRGB(hex)); | ||
}; |