diff --git a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs index ba2e478a..508e8c20 100644 --- a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs +++ b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs @@ -28,7 +28,7 @@ public TestOperationContext() { StateStack.Push(new CurrentGraphicsState()); - CurrentPath = new PdfPath(CurrentTransformationMatrix); + CurrentPath = new PdfPath(); ColorSpaceContext = new ColorSpaceContext(GetCurrentState); } diff --git a/src/UglyToad.PdfPig/Graphics/ColorSpaceContext.cs b/src/UglyToad.PdfPig/Graphics/ColorSpaceContext.cs index 160bf342..eb380e85 100644 --- a/src/UglyToad.PdfPig/Graphics/ColorSpaceContext.cs +++ b/src/UglyToad.PdfPig/Graphics/ColorSpaceContext.cs @@ -76,13 +76,37 @@ public void SetStrokingColorGray(decimal gray) { CurrentStrokingColorSpace = ColorSpace.DeviceGray; - currentStateFunc().CurrentStrokingColor = new GrayColor(gray); + + if (gray == 0) + { + currentStateFunc().CurrentStrokingColor = GrayColor.Black; + } + else if (gray == 1) + { + currentStateFunc().CurrentStrokingColor = GrayColor.White; + } + else + { + currentStateFunc().CurrentStrokingColor = new GrayColor(gray); + } } public void SetStrokingColorRgb(decimal r, decimal g, decimal b) { CurrentStrokingColorSpace = ColorSpace.DeviceRGB; - currentStateFunc().CurrentStrokingColor = new RGBColor(r, g, b); + + if (r == 0 && g == 0 && b == 0) + { + currentStateFunc().CurrentStrokingColor = RGBColor.Black; + } + else if (r == 1 && g == 1 && b == 1) + { + currentStateFunc().CurrentStrokingColor = RGBColor.White; + } + else + { + currentStateFunc().CurrentStrokingColor = new RGBColor(r, g, b); + } } public void SetStrokingColorCmyk(decimal c, decimal m, decimal y, decimal k) @@ -94,13 +118,37 @@ public void SetNonStrokingColorGray(decimal gray) { CurrentNonStrokingColorSpace = ColorSpace.DeviceGray; - currentStateFunc().CurrentNonStrokingColor = new GrayColor(gray); + + if (gray == 0) + { + currentStateFunc().CurrentNonStrokingColor = GrayColor.Black; + } + else if (gray == 1) + { + currentStateFunc().CurrentNonStrokingColor = GrayColor.White; + } + else + { + currentStateFunc().CurrentNonStrokingColor = new GrayColor(gray); + } } public void SetNonStrokingColorRgb(decimal r, decimal g, decimal b) { CurrentNonStrokingColorSpace = ColorSpace.DeviceRGB; - currentStateFunc().CurrentNonStrokingColor = new RGBColor(r, g, b); + + if (r == 0 && g == 0 && b == 0) + { + currentStateFunc().CurrentNonStrokingColor = RGBColor.Black; + } + else if (r == 1 && g == 1 && b == 1) + { + currentStateFunc().CurrentNonStrokingColor = RGBColor.White; + } + else + { + currentStateFunc().CurrentNonStrokingColor = new RGBColor(r, g, b); + } } public void SetNonStrokingColorCmyk(decimal c, decimal m, decimal y, decimal k) diff --git a/src/UglyToad.PdfPig/Graphics/Colors/RGBColor.cs b/src/UglyToad.PdfPig/Graphics/Colors/RGBColor.cs index ce8e5a11..12775c56 100644 --- a/src/UglyToad.PdfPig/Graphics/Colors/RGBColor.cs +++ b/src/UglyToad.PdfPig/Graphics/Colors/RGBColor.cs @@ -1,34 +1,47 @@ namespace UglyToad.PdfPig.Graphics.Colors { + using System; + using System.Collections.Generic; + /// /// A color with red, green and blue components. /// - internal class RGBColor : IColor + internal class RGBColor : IColor, IEquatable { + /// + /// RGB Black value (all 0). + /// public static RGBColor Black = new RGBColor(0, 0, 0); + + /// + /// RGB White value (all 1). + /// public static RGBColor White = new RGBColor(1, 1, 1); /// public ColorSpace ColorSpace { get; } = ColorSpace.DeviceRGB; /// - /// The red value. + /// The red value between 0 and 1. /// public decimal R { get; } /// - /// The green value. + /// The green value between 0 and 1. /// public decimal G { get; } /// - /// The blue value. + /// The blue value between 0 and 1. /// public decimal B { get; } /// /// Create a new . /// + /// The red value between 0 and 1. + /// The green value between 0 and 1. + /// The blue value between 0 and 1. public RGBColor(decimal r, decimal g, decimal b) { R = r; @@ -37,11 +50,39 @@ } /// - public (decimal r, decimal g, decimal b) ToRGBValues() + public (decimal r, decimal g, decimal b) ToRGBValues() => (R, G, B); + + /// + public override bool Equals(object obj) { - return (R, G, B); + if (obj is RGBColor color) + { + return Equals(color); + } + + return false; } + /// + /// + /// Whether 2 RGB colors are equal across all channels. + /// + public bool Equals(RGBColor other) + { + return other != null && + R == other.R && + G == other.G && + B == other.B; + } + + /// + public override int GetHashCode() => (R, G, B).GetHashCode(); + + public static bool operator ==(RGBColor color1, RGBColor color2) => + EqualityComparer.Default.Equals(color1, color2); + + public static bool operator !=(RGBColor color1, RGBColor color2) => !(color1 == color2); + /// public override string ToString() {