mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
use single instances of black and white for rgb/gray colors
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
public TestOperationContext()
|
||||
{
|
||||
StateStack.Push(new CurrentGraphicsState());
|
||||
CurrentPath = new PdfPath(CurrentTransformationMatrix);
|
||||
CurrentPath = new PdfPath();
|
||||
ColorSpaceContext = new ColorSpaceContext(GetCurrentState);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,34 +1,47 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Colors
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// A color with red, green and blue components.
|
||||
/// </summary>
|
||||
internal class RGBColor : IColor
|
||||
internal class RGBColor : IColor, IEquatable<RGBColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// RGB Black value (all 0).
|
||||
/// </summary>
|
||||
public static RGBColor Black = new RGBColor(0, 0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// RGB White value (all 1).
|
||||
/// </summary>
|
||||
public static RGBColor White = new RGBColor(1, 1, 1);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ColorSpace ColorSpace { get; } = ColorSpace.DeviceRGB;
|
||||
|
||||
/// <summary>
|
||||
/// The red value.
|
||||
/// The red value between 0 and 1.
|
||||
/// </summary>
|
||||
public decimal R { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The green value.
|
||||
/// The green value between 0 and 1.
|
||||
/// </summary>
|
||||
public decimal G { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The blue value.
|
||||
/// The blue value between 0 and 1.
|
||||
/// </summary>
|
||||
public decimal B { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="RGBColor"/>.
|
||||
/// </summary>
|
||||
/// <param name="r">The red value between 0 and 1.</param>
|
||||
/// <param name="g">The green value between 0 and 1.</param>
|
||||
/// <param name="b">The blue value between 0 and 1.</param>
|
||||
public RGBColor(decimal r, decimal g, decimal b)
|
||||
{
|
||||
R = r;
|
||||
@@ -37,11 +50,39 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public (decimal r, decimal g, decimal b) ToRGBValues()
|
||||
public (decimal r, decimal g, decimal b) ToRGBValues() => (R, G, B);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return (R, G, B);
|
||||
if (obj is RGBColor color)
|
||||
{
|
||||
return Equals(color);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Whether 2 RGB colors are equal across all channels.
|
||||
/// </summary>
|
||||
public bool Equals(RGBColor other)
|
||||
{
|
||||
return other != null &&
|
||||
R == other.R &&
|
||||
G == other.G &&
|
||||
B == other.B;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode() => (R, G, B).GetHashCode();
|
||||
|
||||
public static bool operator ==(RGBColor color1, RGBColor color2) =>
|
||||
EqualityComparer<RGBColor>.Default.Equals(color1, color2);
|
||||
|
||||
public static bool operator !=(RGBColor color1, RGBColor color2) => !(color1 == color2);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user