Skip to content

Commit 4ae28bc

Browse files
authored
Merge pull request #566 from KristofferC/kc/stackoverflow
avoid stackoverflow when passing two types that can not be promoted to a matching types in `mean_hue`
2 parents 3964b53 + 68c8bcf commit 4ae28bc

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/utilities.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,17 @@ When the hue difference is exactly 180 degrees, which of the two mean hues is
395395
returned depends on the implementation. In other words, it may vary by the color
396396
type and version.
397397
"""
398-
function mean_hue(h1::T, h2::T) where {T <: Real}
398+
mean_hue(a, b) = _mean_hue(promote(a, b)...)
399+
400+
_mean_hue(a, b) = error("input arguments to `mean_hue` could not promote to valid input types")
401+
function _mean_hue(h1::T, h2::T) where {T <: Real}
399402
@fastmath hmin, hmax = minmax(h1, h2)
400403
d = 180 - normalize_hue(hmin - hmax - 180)
401404
F = typeof(zero(T) / 2)
402405
mh = muladd(F(0.5), d, hmin)
403406
return mh < 0 ? mh + 360 : mh
404407
end
405-
@inline function mean_hue(a::C, b::C) where {Cb <: Union{Lab, Luv, Oklab},
408+
@inline function _mean_hue(a::C, b::C) where {Cb <: Union{Lab, Luv, Oklab},
406409
C <: Union{Cb, AlphaColor{Cb}, ColorAlpha{Cb}}}
407410
a1, b1, a2, b2 = comp2(a), comp3(a), comp2(b), comp3(b)
408411
c1, c2 = chroma(a), chroma(b)
@@ -422,15 +425,14 @@ end
422425
mb = muladd(k2, b1, k1 * b2)
423426
hue(Cb(zero(ma), ma, mb))
424427
end
425-
function mean_hue(a::C, b::C) where {Cb <: Union{LCHab, LCHuv, Oklch},
428+
function _mean_hue(a::C, b::C) where {Cb <: Union{LCHab, LCHuv, Oklch},
426429
C <: Union{Cb, AlphaColor{Cb}, ColorAlpha{Cb}}}
427-
mean_hue(a.c == 0 ? b.h : a.h, b.c == 0 ? a.h : b.h)
430+
_mean_hue(a.c == 0 ? b.h : a.h, b.c == 0 ? a.h : b.h)
428431
end
429-
function mean_hue(a::C, b::C) where {Cb <: Union{HSV, HSL, HSI},
432+
function _mean_hue(a::C, b::C) where {Cb <: Union{HSV, HSL, HSI},
430433
C <: Union{Cb, AlphaColor{Cb}, ColorAlpha{Cb}}}
431-
mean_hue(a.s == 0 ? b.h : a.h, b.s == 0 ? a.h : b.h)
434+
_mean_hue(a.s == 0 ? b.h : a.h, b.s == 0 ? a.h : b.h)
432435
end
433-
mean_hue(a, b) = mean_hue(promote(a, b)...)
434436

435437
_delta_h_th(T) = zero(T)
436438
_delta_h_th(::Type{Float32}) = 0.1f0

0 commit comments

Comments
 (0)