Repository navigation
MSC() is strange #349
Description
Activity
- Validity
Although it is not the main cause, I found a discrepancy in the gamma correction:
Using
srgb_compand(v),MSC(h)passes the following test.for hsv_h in 0:0.1:360 hsv = HSV(hsv_h,1.0,1.0) # most saturated lch = convert(LCHuv, hsv) msc = MSC(lch.h) @test msc ≈ lch atol=1e-6 end
So, the following patch may not be necessary.
Lines 195 to 203 in e7f4723
#check if we are directly on the edge of the RGB cube (within some tolerance) for edge in [h0, h1, h2, h3, h4, h5] if edge - 200eps() < h < edge + 200eps() col[p] = edge in [h0, h2, h4] ? 0.0 : 1.0 col[o] = 0.0 col[t] = 1.0 return convert(LCHuv, RGB(col[1],col[2],col[3])) end end - Validity
The disagreement is found in not only light colors but also purple colors:
It turns out it was a simple reason.
MSC(h, l)uses the linear interpolation.
Lines 259 to 260 in e7f4723
a=(pend.l-l)/(pend.l-pmid.l) a*(pmid.c-pend.c)+pend.c The sRGB gamut is not triangular in L-C section, especially in blue to red via purple, as shown in Figure 3 from "Generating Color Palettes using Intuitive Parameters".
hue: 0° hue: 280° 

see also: https://commons.wikimedia.org/wiki/File:SRGB_gamut_within_CIELUV_color_space_mesh.webm
It may be a good approximation for generating colormaps. However, I doubt it is sufficient for any purpose.
Really nice diagnosis and analysis, @kimikage. Your instincts are excellent, when you think you have a solution you like I look forward to your proposal.
After hours of trial and error, I found that, somehow, the ugly function
find_maximum_chroma(simplified version) is faster than any other methods I tried, even though it requires more than 20 iterations. 😅
Did the tail call optimization work well?Did the tail call optimization work well?
Julia doesn't offer intrinsic support for TCO, except in cases where the result can be computed at compile time. But here it doesn't matter because most of the time is taken up by the
v^(1/2.4)operation insrgb_compand.Reacted by kimikage- added a commit that references this issue
on Sep 17, 2019 Probably
maximize_chromaneeds a lot of magic numbers. For this reason, I want to settle the RGB conversion matrix first.However, it is not strictly "the first". It is related to the problem of gamut (cf. #372 (comment)).
Moreover, it is related to the problem withrandin ColorTypes.jl (cf. JuliaGraphics/ColorTypes.jl#125, JuliaGraphics/ColorTypes.jl#140).
MSC()have been introduced to realize the colormap function since 3e2dcf1 .I think
MSC()is strange in some respects.1. Name
As
MSC()is not a constructor but an ordinaryl function, it is to be desired that the name is lowercase to follow the Style Guide, even though the original reference paper usesMSC().In particular, I think the naming is important because
MSCis exported.However, renaming
MSCtomsc,most_saturated_color,maximally_saturated_coloror something else is not sufficient to solve the problems. The reasons are as follows.By the way, although "colorfulness", "chroma" and "saturation" are often used loosely, the term "chroma" is used in the CIELAB and CIELUV color spaces. Perhaps "saturation" may mean that it is saturated in sRGB HSV space, though.
2. Return value
MSC(h)returnsLCHuvcolor, butMSC(h, l)returns saturation value.So, if we follow the behavior, they must have different names and especially the latter should be renamed.
Do we really need two different functions?
3. Color space
The current
MSC()calculates inLuv(LCHuv) color space. I think the behavior is OK, but the nameMSC()and its arguments are not informative about the color space.The function to get the maximally saturated color in
Labcolor space, might improvedistinguishable_colors().When we add such a variant function or method, we should modify the interface.
4. Validity
Edit: see added comments below
I wrote the following ugly function using binary search:
And then I got some strange results:
The disagreement is found in not only light colors but also purple colors:
I don't know whether it is a feature. I have not investigated the cause of it.Although it is not the main cause, I found a discrepancy in the gamma correction:
Colors.jl/src/algorithms.jl
Lines 233 to 234 in e7f4723
Colors.jl/src/conversions.jl
Lines 76 to 78 in e7f4723
Proposal
What about a new function
maximize_chroma(c::Union{Luv,LCHuv}; ltol=0, htol=0)?Where
ltol: lightness tolerance,htol: hue tolerance.The current
MSC()will be redefined as:Well, it is still in the planning stage and I am not ready for implementing it.