Skip to content

Commit 8ff7d65

Browse files
committed
add ColorTests.cs; improve CompareImages warning
1 parent 6cc8e6b commit 8ff7d65

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

Pinta.Core/Effects/ColorBgra.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public static ColorBgra Blend (ReadOnlySpan<ColorBgra> colors)
485485
/// <returns>A ColorBgra value in premultiplied alpha form</returns>
486486
public readonly ColorBgra ToPremultipliedAlpha () => FromBgra ((byte) (B * A / 255), (byte) (G * A / 255), (byte) (R * A / 255), A);
487487

488-
public static bool ColorsWithinTolerance (ColorBgra a, ColorBgra b, int tolerance)
488+
public static int ColorDifference (ColorBgra a, ColorBgra b)
489489
{
490490
int diffR = a.R - b.R;
491491
int diffG = a.G - b.G;
@@ -498,10 +498,11 @@ public static bool ColorsWithinTolerance (ColorBgra a, ColorBgra b, int toleranc
498498
int summandA = diffA * diffA;
499499

500500
int sum = summandR + summandG + summandB + summandA;
501-
502-
return sum <= tolerance * tolerance * 4;
501+
return sum;
503502
}
504503

504+
public static bool ColorsWithinTolerance (ColorBgra a, ColorBgra b, int tolerance) => ColorDifference (a, b) <= tolerance * tolerance * 4;
505+
505506
/// <summary>
506507
/// Brings the color channels from premultiplied alpha in straight alpha form.
507508
/// This is required for direct memory manipulation when reading from Cairo surfaces
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using Gtk;
3+
using NUnit.Framework;
4+
using Pango;
5+
using Color = Cairo.Color;
6+
7+
namespace Pinta.Core.Tests;
8+
9+
[TestFixture]
10+
internal sealed class ColorTests
11+
{
12+
[TestCase (1, 0, 0, 0, 1, 1)]
13+
[TestCase (0, 1, 0, 120, 1, 1)]
14+
[TestCase (0, 0, 1, 240, 1, 1)]
15+
[TestCase (0, 0.5, 1, 210, 1, 1)]
16+
[TestCase (0.2, 0.5, 0.25, 130, 0.6, 0.5)]
17+
public void ColorToHsv (double r, double g, double b, double h, double s, double v)
18+
{
19+
Color c = new (r, g, b);
20+
HsvColor hsv = c.ToHsv ();
21+
Assert.That (hsv, Is.EqualTo (new HsvColor (h, s, v)));
22+
Color c2 = hsv.ToColor ();
23+
// assert reversibility; color > hsv > color retains same info
24+
// floating point rounding
25+
c2 = new (Math.Round (c2.R, 4), Math.Round (c2.G, 4), Math.Round (c2.B, 4));
26+
Assert.That (c2, Is.EqualTo (c));
27+
}
28+
29+
[TestCase ("FFFFFF", 1, 1, 1, 1)]
30+
[TestCase ("FFFF", 1, 1, 1, 1)]
31+
[TestCase ("FFF", 1, 1, 1, 1)]
32+
[TestCase ("#FFFFFF", 1, 1, 1, 1)]
33+
[TestCase ("FFFF", 1, 1, 1, 1)]
34+
[TestCase ("#FFF", 1, 1, 1, 1)]
35+
[TestCase ("CC33AA99", 0.8, 0.2, 0.6667, 0.6)]
36+
[TestCase ("#CC33AA99", 0.8, 0.2, 0.6667, 0.6)]
37+
[TestCase ("C3A9", 0.8, 0.2, 0.6667, 0.6)]
38+
[TestCase ("C3A", 0.8, 0.2, 0.6667, 1)]
39+
public void Hex (string hex, double r, double g, double b, double a)
40+
{
41+
var hc = Color.FromHex (hex)!.Value;
42+
hc = new (Math.Round (hc.R, 4), Math.Round (hc.G, 4), Math.Round (hc.B, 4));
43+
var c = new Color (r, g, b);
44+
Assert.That (hc, Is.EqualTo (c));
45+
}
46+
}

tests/Pinta.Effects.Tests/Utilities.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public static void CompareImages (ImageSurface result, ImageSurface expected, in
5555
++diffs;
5656

5757
// Display info about the first few failures.
58+
5859
if (diffs <= 10)
59-
Assert.Warn ($"Difference at pixel {i}, got {result_pixels[i]} vs {expected_pixels[i]}");
60+
Assert.Warn ($"Difference at pixel {i}, got {result_pixels[i]} vs {expected_pixels[i]}, diff. of {ColorBgra.ColorDifference (result_pixels[i], expected_pixels[i])}");
6061
}
6162
}
6263

0 commit comments

Comments
 (0)