-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gamma algorithm to handle negative value
- Loading branch information
Showing
3 changed files
with
70 additions
and
14 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,15 +1,21 @@ | ||
export function srgb_to_linear(srgb: number) { | ||
if (srgb <= 0.04045) { | ||
let srgb_sign = srgb < 0 ? -1 : 1; | ||
let srgb_abs = srgb * srgb_sign; | ||
|
||
if (srgb_abs <= 0.04045) { | ||
return srgb / 12.92; | ||
} else { | ||
return Math.pow((srgb + 0.055) / 1.055, 2.4); | ||
return srgb_sign * Math.pow((srgb_abs + 0.055) / 1.055, 2.4); | ||
} | ||
} | ||
|
||
export function linear_to_srgb(linear: number) { | ||
if (linear <= 0.0031308) { | ||
let linear_sign = linear < 0 ? -1 : 1; | ||
let linear_abs = linear * linear_sign; | ||
|
||
if (linear_abs <= 0.0031308) { | ||
return linear * 12.92; | ||
} else { | ||
return 1.055 * Math.pow(linear, 1 / 2.4) - 0.055; | ||
return linear_sign * (1.055 * Math.pow(linear_abs, 1 / 2.4) - 0.055); | ||
} | ||
} |
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