add and document core classes for colorspace information

This commit is contained in:
Eliot Jones
2019-08-04 12:57:06 +01:00
parent f07ab7d2c3
commit 1d551d6de3
6 changed files with 237 additions and 9 deletions

View File

@@ -77,6 +77,9 @@
"UglyToad.PdfPig.Geometry.PdfPoint",
"UglyToad.PdfPig.Geometry.PdfLine",
"UglyToad.PdfPig.Geometry.PdfRectangle",
"UglyToad.PdfPig.Graphics.Colors.ColorSpace",
"UglyToad.PdfPig.Graphics.Colors.ColorSpaceExtensions",
"UglyToad.PdfPig.Graphics.Colors.ColorSpaceFamily",
"UglyToad.PdfPig.Graphics.Core.LineCapStyle",
"UglyToad.PdfPig.Graphics.Core.LineDashPattern",
"UglyToad.PdfPig.Graphics.Core.LineJoinStyle",

View File

@@ -1,2 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CIE/@EntryIndexedValue">CIE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CMYK/@EntryIndexedValue">CMYK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ICC/@EntryIndexedValue">ICC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RGB/@EntryIndexedValue">RGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=XY/@EntryIndexedValue">XY</s:String></wpf:ResourceDictionary>

View File

@@ -1,9 +0,0 @@
namespace UglyToad.PdfPig.Core
{
using System.IO;
internal interface ICosStreamWriter
{
void WriteToPdfStream(BinaryWriter output);
}
}

View File

@@ -0,0 +1,74 @@
namespace UglyToad.PdfPig.Graphics.Colors
{
/// <summary>
/// Color values in a PDF are interpreted according to the current color space.
/// Color spaces enable a PDF to specify abstract colors in a device independent way.
/// </summary>
public enum ColorSpace
{
/// <summary>
/// Grayscale. Controls the intensity of achromatic light on a scale from black to white.
/// </summary>
DeviceGray = 0,
/// <summary>
/// RGB. Controls the intensities of red, green and blue light.
/// </summary>
DeviceRGB = 1,
/// <summary>
/// CMYK. Controls the concentrations of cyan, magenta, yellow and black (K) inks.
/// </summary>
DeviceCMYK = 2,
/// <summary>
/// CIE (Commission Internationale de l'Éclairage) colorspace.
/// Specifies color related to human visual perception with the aim of producing consistent color on different output devices.
/// CalGray - Special case of the CIE colorspace using a single channel (A) and a single transformation.
/// A represents the gray component of a calibrated gray space in the range 0 to 1.
/// </summary>
CalGray = 3,
/// <summary>
/// CIE (Commission Internationale de l'Éclairage) colorspace.
/// Specifies color related to human visual perception with the aim of producing consistent color on different output devices.
/// CalRGB - A CIE ABC color space with a single transformation.
/// A, B and C represent red, green and blue color values in the range 0 to 1.
/// </summary>
CalRGB = 4,
/// <summary>
/// CIE (Commission Internationale de l'Éclairage) colorspace.
/// Specifies color related to human visual perception with the aim of producing consistent color on different output devices.
/// Lab - A CIE ABC color space with two transforms. A, B and C represent the L*, a* and b*
/// components of a CIE 1976 L*a*b* space. The range of A (L*) is 0 to 100.
/// The range of B (a*) and C (b*) are defined by the Range of the color space.
/// </summary>
Lab = 5,
/// <summary>
/// ICC (International Color Consortium) colorspace.
/// ICC - Colorspace specified by a sequence of bytes which are interpreted according to the
/// ICC specification.
/// </summary>
ICCBased = 6,
/// <summary>
/// An Indexed color space allows a PDF content stream to use small integers as indices into a color map or color table of arbitrary colors in some other space.
/// A PDF consumer application treats each sample value as an index into the color table and uses the color value it finds there.
/// </summary>
Indexed = 7,
/// <summary>
/// Enables a PDF content stream to paint an area with a pattern rather than a single color.
/// The pattern may be either a tiling pattern (type 1) or a shading pattern (type 2).
/// </summary>
Pattern = 8,
/// <summary>
/// Provides a means for specifying the use of additional colorants or for isolating the control of individual color components of
/// a device color space for a subtractive device.
/// When such a space is the current color space, the current color is a single-component value, called a tint,
/// that controls the application of the given colorant or color components only.
/// </summary>
Separation = 9,
/// <summary>
/// Can contain an arbitrary number of color components. They provide greater flexibility than is possible with standard device color
/// spaces such as <see cref="DeviceCMYK"/> or with individual <see cref="Separation"/> color spaces.
/// For example, it is possible to create a DeviceN color space consisting of only the cyan, magenta, and yellow color components,
/// with the black component excluded.
/// </summary>
DeviceN = 10
}
}

View File

@@ -0,0 +1,130 @@
namespace UglyToad.PdfPig.Graphics.Colors
{
using System;
using Tokens;
/// <summary>
/// Provides utility extension methods for dealing with <see cref="ColorSpace"/>s.
/// </summary>
public static class ColorSpaceExtensions
{
/// <summary>
/// Gets the corresponding <see cref="ColorSpaceFamily"/> for a given <see cref="ColorSpace"/>.
/// </summary>
public static ColorSpaceFamily GetFamily(this ColorSpace colorSpace)
{
switch (colorSpace)
{
case ColorSpace.DeviceGray:
case ColorSpace.DeviceRGB:
case ColorSpace.DeviceCMYK:
return ColorSpaceFamily.Device;
case ColorSpace.CalGray:
case ColorSpace.CalRGB:
case ColorSpace.Lab:
case ColorSpace.ICCBased:
return ColorSpaceFamily.CIEBased;
case ColorSpace.Indexed:
case ColorSpace.Pattern:
case ColorSpace.Separation:
case ColorSpace.DeviceN:
return ColorSpaceFamily.Special;
default:
throw new ArgumentException($"Unrecognized colorspace: {colorSpace}.");
}
}
/// <summary>
/// Maps from a <see cref="NameToken"/> to the corresponding <see cref="ColorSpace"/> if one exists.
/// </summary>
public static bool TryMapToColorSpace(this NameToken name, out ColorSpace colorspace)
{
colorspace = ColorSpace.DeviceGray;
if (name.Data == NameToken.Devicegray.Data)
{
colorspace = ColorSpace.DeviceGray;
}
else if (name.Data == NameToken.Devicergb.Data)
{
colorspace = ColorSpace.DeviceRGB;
}
else if (name.Data == NameToken.Devicecmyk.Data)
{
colorspace = ColorSpace.DeviceCMYK;
}
else if (name.Data == NameToken.Calgray.Data)
{
colorspace = ColorSpace.CalGray;
}
else if (name.Data == NameToken.Calrgb.Data)
{
colorspace = ColorSpace.CalRGB;
}
else if (name.Data == NameToken.Lab.Data)
{
colorspace = ColorSpace.Lab;
}
else if (name.Data == NameToken.Iccbased.Data)
{
colorspace = ColorSpace.ICCBased;
}
else if (name.Data == NameToken.Indexed.Data)
{
colorspace = ColorSpace.Indexed;
}
else if (name.Data == NameToken.Pattern.Data)
{
colorspace = ColorSpace.Pattern;
}
else if (name.Data == NameToken.Separation.Data)
{
colorspace = ColorSpace.Separation;
}
else if (name.Data == NameToken.Devicen.Data)
{
colorspace = ColorSpace.DeviceN;
}
else
{
return false;
}
return true;
}
/// <summary>
/// Gets the corresponding <see cref="NameToken"/> for a given <see cref="ColorSpace"/>.
/// </summary>
public static NameToken ToNameToken(this ColorSpace colorSpace)
{
switch (colorSpace)
{
case ColorSpace.DeviceGray:
return NameToken.Devicegray;
case ColorSpace.DeviceRGB:
return NameToken.Devicergb;
case ColorSpace.DeviceCMYK:
return NameToken.Devicecmyk;
case ColorSpace.CalGray:
return NameToken.Calgray;
case ColorSpace.CalRGB:
return NameToken.Calrgb;
case ColorSpace.Lab:
return NameToken.Lab;
case ColorSpace.ICCBased:
return NameToken.Iccbased;
case ColorSpace.Indexed:
return NameToken.Indexed;
case ColorSpace.Pattern:
return NameToken.Pattern;
case ColorSpace.Separation:
return NameToken.Separation;
case ColorSpace.DeviceN:
return NameToken.Devicen;
default:
throw new ArgumentException($"Unrecognized colorspace: {colorSpace}.");
}
}
}
}

View File

@@ -0,0 +1,26 @@
namespace UglyToad.PdfPig.Graphics.Colors
{
/// <summary>
/// <see cref="ColorSpace"/>s can be classified into colorspace families.
/// <see cref="ColorSpace"/>s within the same family share general characteristics.
/// </summary>
public enum ColorSpaceFamily
{
/// <summary>
/// Device colorspaces directly specify colors or shades of gray that the output device
/// should produce.
/// </summary>
Device,
/// <summary>
/// CIE-based color spaces are based on an international standard for color specification created by
/// the Commission Internationale de l'Éclairage (International Commission on Illumination) (CIE).
/// These spaces specify colors in a way that is independent of the characteristics of any particular output device.
/// </summary>
CIEBased,
/// <summary>
/// Special color spaces add features or properties to an underlying color space.
/// They include facilities for patterns, color mapping, separations, and high-fidelity and multitone color.
/// </summary>
Special
}
}