Skip to content

Commit

Permalink
lightness
Browse files Browse the repository at this point in the history
  • Loading branch information
Variable-ind committed Oct 25, 2024
1 parent f379df0 commit 04ef4e6
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Palette/Palette.gd
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,29 @@ func sort(option: Palettes.SortOptions) -> void:
Palettes.SortOptions.VALUE:
sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.v < b.color.v
Palettes.SortOptions.LIGHTNESS:
# Code inspired from:
# gdlint: ignore=max-line-length
# https://github.com/bottosson/bottosson.github.io/blob/master/misc/colorpicker/colorconversion.js#L519
sort_method = func(a: PaletteColor, b: PaletteColor):
var rgb_a: Array[float] = [a.color.r, a.color.g, a.color.b]
var rgb_b: Array[float] = [b.color.r, b.color.g, b.color.b]
var luminance_a = (rgb_a.max() + rgb_a.min()) / 2.0
var luminance_b = (rgb_b.max() + rgb_b.min()) / 2.0
return luminance_a < luminance_b
# function that returns OKHSL luminance
var lum : Callable = func(c: Color):
var l = 0.4122214708 * (c.r) + 0.5363325363 * (c.g) + 0.0514459929 * (c.b)
var m = 0.2119034982 * (c.r) + 0.6806995451 * (c.g) + 0.1073969566 * (c.b)
var s = 0.0883024619 * (c.r) + 0.2817188376 * (c.g) + 0.6299787005 * (c.b);
var l_ = pow(l, 1/3.0);
var m_ = pow(m, 1/3.0);
var s_ = pow(s, 1/3.0);
var oklab_l = 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_
# calculating toe
var k_1 = 0.206
var k_2 = 0.03
var k_3 = (1+k_1)/(1+k_2)
return 0.5 * (
k_3 * oklab_l - k_1
+ sqrt(
(k_3 * oklab_l - k_1) * (k_3 * oklab_l - k_1) + 4 * k_2 * k_3 * oklab_l)
)
return lum.call(a.color.srgb_to_linear()) < lum.call(b.color.srgb_to_linear())
Palettes.SortOptions.RED:
sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.r < b.color.r
Palettes.SortOptions.GREEN:
Expand Down

0 comments on commit 04ef4e6

Please sign in to comment.