Skip to content

Commit

Permalink
Fix DE_CMC color difference metric
Browse files Browse the repository at this point in the history
This uses the first argument of `colordiff` as the reference color.
  • Loading branch information
kimikage committed Mar 2, 2021
1 parent cf7b34b commit 4dcbc6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
24 changes: 8 additions & 16 deletions src/differences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Construct a metric using the CMC equation (CMC l:c), with weighting parameters
The `DE_CMC` is a quasimetric, i.e. violates symmetry. Therefore,
`colordiff(a, b, metric=DE_CMC())` may not equal to
`colordiff(b, a, metric=DE_CMC())`.
The first argument of `colordiff` is taken as the reference (standard)
color.
"""
DE_CMC() = DE_CMC(1,1)

Expand Down Expand Up @@ -325,32 +327,22 @@ function _colordiff(ai::Color, bi::Color, m::DE_CMC)
# Calculate H* from C*'s and h
dh = 2 * sqrt(a.c * b.c) * sind(dh/2)

# Find the mean value of the inputs to use as the "standard"
ml, mc = (a.l + b.l)/2, (a.c + b.c)/2 # TODO: use `a.l` and `a.c` instead

# Calculate mean hue value
if a.c * b.c == 0
mh = a.h + b.h
else
mh = mean_hue(a.h, b.h)
end

# L* adjustment term
if (a.l <= 16)
sl = 0.511
else
sl = 0.040975*ml/(1+0.01765*ml)
sl = 0.040975 * a.l / (1 + 0.01765 * a.l)
end

# C* adjustment term
sc = 0.0638*mc/(1+0.0131*mc)+0.638
sc = 0.0638 * a.c / (1 + 0.0131 * a.c) + 0.638

f = sqrt((mc^4)/(mc^4 + 1900))
f = sqrt(a.c^4 / (a.c^4 + 1900))

if (mh >= 164 && mh < 345)
t = 0.56 + abs(0.2*cosd(mh+168))
if 164 <= a.h <= 345
t = 0.56 + abs(0.2 * cosd(a.h + 168))
else
t = 0.36 + abs(0.4*cosd(mh+35))
t = 0.36 + abs(0.4 * cosd(a.h + 35))
end

# H* adjustment term
Expand Down
3 changes: 1 addition & 2 deletions test/colordiff.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Colors, Test
using Colors, Test, FixedPointNumbers

# Test the colordiff function against example input and output from:
#
Expand Down Expand Up @@ -79,7 +79,6 @@ using Colors, Test
colordiff(p[2], p[1]; metric=metric)) < eps_cdiff, pairs)
end
# triangle inequality
metric isa DE_CMC && continue # FIXME
@test all(p -> colordiff(p[1], p[2]; metric=metric) <=
colordiff(p[1], jl_red; metric=metric) +
colordiff(jl_red, p[2]; metric=metric), pairs)
Expand Down

0 comments on commit 4dcbc6d

Please sign in to comment.