Skip to content

Commit 97ad1de

Browse files
author
Thomas Lindemeier
committed
IMproved fuzzy compare
1 parent f7f94c6 commit 97ad1de

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

Diff for: include/ColorConverter/ColorConverter.hxx

+38-36
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,19 @@ public:
330330

331331
const Scalar fa = 1. / 360.0;
332332

333-
if (fuzzy(max, min))
333+
if (fuzzyCompare(max, min))
334334
hsv[0] = 0;
335-
else if (fuzzy(max, srgb[0]))
335+
else if (fuzzyCompare(max, srgb[0]))
336336
hsv[0] = 60.0 * (0 + (srgb[1] - srgb[2]) * delMax);
337-
else if (fuzzy(max, srgb[1]))
337+
else if (fuzzyCompare(max, srgb[1]))
338338
hsv[0] = 60.0 * (2 + (srgb[2] - srgb[0]) * delMax);
339-
else if (fuzzy(max, srgb[2]))
339+
else if (fuzzyCompare(max, srgb[2]))
340340
hsv[0] = 60.0 * (4 + (srgb[0] - srgb[1]) * delMax);
341341

342342
if (hsv[0] < 0.0)
343343
hsv[0] += 360.0;
344344

345-
if (fuzzy(max, 0.0))
345+
if (fuzzyCompare(max, 0.0))
346346
{
347347
hsv[1] = 0.0;
348348
}
@@ -496,34 +496,6 @@ public:
496496
lab2rgb(v, rgb);
497497
}
498498

499-
private:
500-
const std::array<Scalar, 3U> illuminant;
501-
502-
static Scalar f(Scalar t)
503-
{
504-
return (t > std::pow<Scalar>(6. / 29., 3.))
505-
? std::pow<Scalar>(t, 1. / 3.)
506-
: (1. / 3.) * std::pow<Scalar>(29. / 6., 2.) * t + (4. / 29.);
507-
}
508-
509-
static Scalar fi(Scalar t)
510-
{
511-
return (t > 6. / 29.)
512-
? std::pow<Scalar>(t, 3.)
513-
: 3. * std::pow<Scalar>(6. / 29., 2.) * (t - (4. / 29.));
514-
}
515-
516-
/**
517-
* @brief Simple fuzzy comparison of floating point values.
518-
*
519-
* @return true
520-
* @return false
521-
*/
522-
static bool fuzzy(const Scalar a, const Scalar b)
523-
{
524-
return std::fabs(a - b) < std::numeric_limits<Scalar>::epsilon();
525-
}
526-
527499
/**
528500
* @brief Compute difference of two given colors.
529501
*
@@ -572,7 +544,7 @@ private:
572544
Scalar hpsample = std::atan2(bsample, apsample);
573545
if (hpsample < 0)
574546
hpsample += 2. * Pi;
575-
if (fuzzy((fabs(apsample) + fabs(bsample)), 0.))
547+
if (fuzzyCompare((fabs(apsample) + fabs(bsample)), 0.))
576548
hpsample = 0.;
577549

578550
Scalar dL = (Lsample - Lstd);
@@ -585,7 +557,7 @@ private:
585557
if (dhp < -Pi)
586558
dhp += 2. * Pi;
587559
// set chroma difference to zero if the product of chromas is zero
588-
if (fuzzy(Cpprod, 0.))
560+
if (fuzzyCompare(Cpprod, 0.))
589561
dhp = 0.;
590562

591563
// Note that the defining equations actually need
@@ -613,7 +585,7 @@ private:
613585

614586
// Check if one of the chroma values is zero, in which case set
615587
// mean hue to the sum which is equivalent to other value
616-
if (fuzzy(Cpprod, 0.))
588+
if (fuzzyCompare(Cpprod, 0.))
617589
hp = hpsample + hpstd;
618590

619591
Scalar Lpm502 = (Lp - 50.) * (Lp - 50.);
@@ -635,6 +607,36 @@ private:
635607
return std::sqrt(std::pow((dL / Sl), 2.) + std::pow((dC / Sc), 2.) +
636608
std::pow((dH / Sh), 2.) + RT * (dC / Sc) * (dH / Sh));
637609
}
610+
611+
private:
612+
const std::array<Scalar, 3U> illuminant;
613+
614+
static Scalar f(Scalar t)
615+
{
616+
return (t > std::pow<Scalar>(6. / 29., 3.))
617+
? std::pow<Scalar>(t, 1. / 3.)
618+
: (1. / 3.) * std::pow<Scalar>(29. / 6., 2.) * t + (4. / 29.);
619+
}
620+
621+
static Scalar fi(Scalar t)
622+
{
623+
return (t > 6. / 29.)
624+
? std::pow<Scalar>(t, 3.)
625+
: 3. * std::pow<Scalar>(6. / 29., 2.) * (t - (4. / 29.));
626+
}
627+
628+
/**
629+
* @brief Simple fuzzyCompare comparison of floating point values.
630+
*
631+
* @return true
632+
* @return false
633+
*/
634+
static bool fuzzyCompare(const Scalar a, const Scalar b)
635+
{
636+
constexpr auto FuzzynessFactor = 10.0;
637+
return std::fabs(a - b) <
638+
(FuzzynessFactor * std::numeric_limits<Scalar>::epsilon());
639+
}
638640
};
639641

640642
} // namespace color

0 commit comments

Comments
 (0)