Skip to content

Commit 657e265

Browse files
authored
Merge pull request #338 from johnnychen94/colordiff
enhance Colordiff API
2 parents 0f4c744 + 6b28f27 commit 657e265

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

docs/src/colorspaces.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,18 @@ whitebalance
9595

9696
## Color Difference
9797

98-
The `colordiff()` function gives an approximate value for the difference between two colors.
98+
The `colordiff` function gives an approximate value for the difference between two colors.
9999

100100
```
101101
julia> colordiff(colorant"red", parse(Colorant, HSB(360, 0.75, 1)))
102102
8.178248292426845
103103
```
104104

105-
`colordiff(a::Color, b::Color)`
105+
`colordiff(a::Color, b::Color; metric::DifferenceMetric=DE_2000())`
106106

107-
Evaluate the [CIEDE2000](http://en.wikipedia.org/wiki/Color_difference#CIEDE2000) color difference formula. This gives an approximate measure of the perceptual difference between two colors to a typical viewer. A larger number is returned for increasingly distinguishable colors.
107+
Evaluate the [CIEDE2000](http://en.wikipedia.org/wiki/Color_difference#CIEDE2000) color difference formula by default. This gives an approximate measure of the perceptual difference between two colors to a typical viewer. A larger number is returned for increasingly distinguishable colors.
108108

109-
`colordiff(a::Color, b::Color, m::DifferenceMetric)`
110-
111-
Evaluate the color difference formula specified by the supplied `DifferenceMetric`.
112-
113-
Options are as follows:
109+
Options for `DifferenceMetric` are as follows:
114110

115111
| Option | Action |
116112
| ---------- | ------- |

src/differences.jl

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pow7(x::Integer) = pow7(Float64(x))
9999
const twentyfive7 = pow7(25)
100100

101101
# Delta E 2000
102-
function colordiff(ai::Color, bi::Color, m::DE_2000)
102+
function _colordiff(ai::Color, bi::Color, m::DE_2000)
103103
# Ensure that the input values are in L*a*b* space
104104
a_Lab = convert(Lab, ai)
105105
b_Lab = convert(Lab, bi)
@@ -162,7 +162,7 @@ function colordiff(ai::Color, bi::Color, m::DE_2000)
162162
end
163163

164164
# Delta E94
165-
function colordiff(ai::Color, bi::Color, m::DE_94)
165+
function _colordiff(ai::Color, bi::Color, m::DE_94)
166166

167167
a = convert(LCHab, ai)
168168
b = convert(LCHab, bi)
@@ -192,7 +192,7 @@ function colordiff(ai::Color, bi::Color, m::DE_94)
192192
end
193193

194194
# Metric form of jpc79 color difference equation (mostly obsolete)
195-
function colordiff(ai::Color, bi::Color, m::DE_JPC79)
195+
function _colordiff(ai::Color, bi::Color, m::DE_JPC79)
196196

197197
# Convert directly into LCh
198198
a = convert(LCHab, ai)
@@ -242,7 +242,7 @@ end
242242

243243

244244
# Metric form of the cmc color difference
245-
function colordiff(ai::Color, bi::Color, m::DE_CMC)
245+
function _colordiff(ai::Color, bi::Color, m::DE_CMC)
246246

247247
# Convert directly into LCh
248248
a = convert(LCHab, ai)
@@ -297,7 +297,7 @@ function colordiff(ai::Color, bi::Color, m::DE_CMC)
297297
end
298298

299299
# The BFD color difference equation
300-
function colordiff(ai::Color, bi::Color, m::DE_BFD)
300+
function _colordiff(ai::Color, bi::Color, m::DE_BFD)
301301

302302
# We have to start back in XYZ because BFD uses a different L equation
303303
a_XYZ = convert(XYZ, ai, m.wp)
@@ -357,7 +357,7 @@ function colordiff(ai::Color, bi::Color, m::DE_BFD)
357357
end
358358

359359
# Delta E*ab (the original)
360-
function colordiff(ai::Color, bi::Color, m::DE_AB)
360+
function _colordiff(ai::Color, bi::Color, m::DE_AB)
361361

362362
# Convert directly into L*a*b*
363363
a = convert(Lab, ai)
@@ -377,7 +377,7 @@ end
377377
#
378378
# Returns:
379379
# The DIN99 color difference metric evaluated between a and b.
380-
function colordiff(ai::Color, bi::Color, m::DE_DIN99)
380+
function _colordiff(ai::Color, bi::Color, m::DE_DIN99)
381381

382382
a = convert(DIN99, ai)
383383
b = convert(DIN99, bi)
@@ -387,7 +387,7 @@ function colordiff(ai::Color, bi::Color, m::DE_DIN99)
387387
end
388388

389389
# A color difference formula for the DIN99d uniform colorspace
390-
function colordiff(ai::Color, bi::Color, m::DE_DIN99d)
390+
function _colordiff(ai::Color, bi::Color, m::DE_DIN99d)
391391

392392
a = convert(DIN99d, ai)
393393
b = convert(DIN99d, bi)
@@ -397,7 +397,7 @@ function colordiff(ai::Color, bi::Color, m::DE_DIN99d)
397397
end
398398

399399
# The DIN99o color difference metric evaluated between colors a and b.
400-
function colordiff(ai::Color, bi::Color, m::DE_DIN99o)
400+
function _colordiff(ai::Color, bi::Color, m::DE_DIN99o)
401401

402402
a = convert(DIN99o, ai)
403403
b = convert(DIN99o, bi)
@@ -408,12 +408,18 @@ end
408408

409409
# Default to Delta E 2000
410410
"""
411-
colordiff(a, b)
412-
colordiff(a, b, metric)
411+
colordiff(a, b; metric::DifferenceMetric=DE_2000())
413412
414413
Compute an approximate measure of the perceptual difference between
415414
colors `a` and `b`. Optionally, a `metric` may be supplied, chosen
416415
among `DE_2000` (the default), `DE_94`, `DE_JPC79`, `DE_CMC`,
417416
`DE_BFD`, `DE_AB`, `DE_DIN99`, `DE_DIN99d`, `DE_DIN99o`.
418417
"""
419-
colordiff(ai::Color, bi::Color) = colordiff(ai::Color, bi::Color, DE_2000())
418+
colordiff(ai::Union{Number, Color},
419+
bi::Union{Number, Color};
420+
metric::DifferenceMetric=DE_2000()) = _colordiff(ai, bi, metric)
421+
@deprecate colordiff(ai::Color, bi::Color, metric::DifferenceMetric) colordiff(ai, bi; metric=metric)
422+
423+
_colordiff(ai::AbstractGray, bi::Number, metric::DifferenceMetric) = _colordiff(ai, Gray(bi), metric)
424+
_colordiff(ai::Number, bi::AbstractGray, metric::DifferenceMetric) = _colordiff(Gray(ai), bi, metric)
425+
_colordiff(ai::Number, bi::Number, metric::DifferenceMetric) = _colordiff(Gray(ai), Gray(bi), metric)

test/colordiff.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ using Colors
5151

5252
let a, b
5353
for (i, (a, b, dexpect)) in enumerate(abds)
54-
@test abs(dexpect - colordiff(Lab(a...), Lab(b...), metric)) < eps_cdiff
55-
@test abs(dexpect - colordiff(Lab(b...), Lab(a...), metric)) < eps_cdiff
54+
@test abs(dexpect - colordiff(Lab(a...), Lab(b...); metric=metric)) < eps_cdiff
55+
@test abs(dexpect - colordiff(Lab(b...), Lab(a...); metric=metric)) < eps_cdiff
5656
end
5757
end
5858

59+
a, b = rand(100), rand(100)
60+
@test all(@. colordiff(a, b) == colordiff(Gray(a), b) == colordiff(a, Gray(b)) == colordiff(Gray(a), Gray(b)))
61+
5962
end # @testset

test/din99.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using Colors
3939
# This is not a real test of the color difference metric, but at least
4040
# makes sure it isn't doing anything really crazy.
4141
metric = DE_DIN99()
42-
@test (abs(colordiff(convert(DIN99, Lab(a...)), DIN99(b...), metric)) < diffeps)
42+
@test (abs(colordiff(convert(DIN99, Lab(a...)), DIN99(b...); metric=metric)) < diffeps)
4343
end
4444
end
4545
end # @testset

0 commit comments

Comments
 (0)