-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mix colors in the linear sRGB model
This seems to produce results that are closer to perceptual models. See https://bottosson.github.io/posts/colorwrong/
- Loading branch information
Showing
2 changed files
with
15 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { clamp } from "@keybr/lang"; | ||
import { RgbColor } from "./color-rgb.ts"; | ||
import { rgbGammaToLinear, rgbLinearToGamma } from "./convert-xyz.ts"; | ||
import { type Rgb } from "./types.ts"; | ||
|
||
const u: Rgb = { r: 0, g: 0, b: 0, alpha: 1 }; | ||
const v: Rgb = { r: 0, g: 0, b: 0, alpha: 1 }; | ||
|
||
export function mixColors( | ||
a: Readonly<Rgb>, | ||
b: Readonly<Rgb>, | ||
n: number, | ||
r: number, | ||
): RgbColor { | ||
n = clamp(n, 0, 1); | ||
return new RgbColor( | ||
(b.r - a.r) * n + a.r, | ||
(b.g - a.g) * n + a.g, | ||
(b.b - a.b) * n + a.b, | ||
); | ||
r = clamp(r, 0, 1); | ||
rgbGammaToLinear(a, u); | ||
rgbGammaToLinear(b, v); | ||
u.r = (v.r - u.r) * r + u.r; | ||
u.g = (v.g - u.g) * r + u.g; | ||
u.b = (v.b - u.b) * r + u.b; | ||
u.alpha = (v.alpha - u.alpha) * r + u.alpha; | ||
rgbLinearToGamma(u, v); | ||
return new RgbColor(v.r, v.g, v.b, v.alpha); | ||
} |