/// <summary> /// Checks whether a color mode and a color match. /// </summary> public static XColor EnsureColorMode(PDFColorMode colorMode, XColor color) => #if true colorMode == PDFColorMode.Rgb && color.ColorSpace != XColorSpace.Rgb ? XColor.FromArgb((int)(color.A * 255), color.R, color.G, color.B) : colorMode == PDFColorMode.Cmyk && color.ColorSpace != XColorSpace.Cmyk ? XColor.FromCmyk(color.A, color.C, color.M, color.Y, color.K) : color;
public static string InappropriateColorSpace(PDFColorMode colorMode, XColorSpace colorSpace) { string mode; switch (colorMode) { case PDFColorMode.Rgb: mode = "RGB"; break; case PDFColorMode.Cmyk: mode = "CMYK"; break; default: mode = "(undefined)"; break; } string space; switch (colorSpace) { case XColorSpace.Rgb: space = "RGB"; break; case XColorSpace.Cmyk: space = "CMYK"; break; case XColorSpace.GrayScale: space = "grayscale"; break; default: space = "(undefined)"; break; } return(String.Format("The document requires color mode {0}, but a color is defined using {1}. " + "Use only colors that match the color mode of the PDF document", mode, space)); }
/// <summary> /// Converts an XColor into a string with up to 3 decimal digits and a decimal point. /// </summary> public static string ToString(XColor color, PDFColorMode colorMode) { const string format = Config.SignificantFigures3; // If not defined let color decide if (colorMode == PDFColorMode.Undefined) { colorMode = color.ColorSpace == XColorSpace.Cmyk ? PDFColorMode.Cmyk : PDFColorMode.Rgb; } switch (colorMode) { case PDFColorMode.Cmyk: return(String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}", color.C, color.M, color.Y, color.K)); default: return(String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "}", color.R / 255.0, color.G / 255.0, color.B / 255.0)); } }
/// <summary> /// Setups the shading from the specified brush. /// </summary> internal void SetupFromBrush(XLinearGradientBrush brush, XGraphicsPDFRenderer renderer) { if (brush == null) { throw new ArgumentNullException("brush"); } PDFColorMode colorMode = _document.Options.ColorMode; XColor color1 = ColorSpaceHelper.EnsureColorMode(colorMode, brush._color1); XColor color2 = ColorSpaceHelper.EnsureColorMode(colorMode, brush._color2); PDFDictionary function = new PDFDictionary(); Elements[Keys.ShadingType] = new PDFInteger(2); Elements[Keys.ColorSpace] = colorMode != PDFColorMode.Cmyk ? new PDFName("/DeviceRGB") : new PDFName("/DeviceCMYK"); double x1 = 0, y1 = 0, x2 = 0, y2 = 0; if (brush._useRect) { XPoint pt1 = renderer.WorldToView(brush._rect.TopLeft); XPoint pt2 = renderer.WorldToView(brush._rect.BottomRight); switch (brush._linearGradientMode) { case XLinearGradientMode.Horizontal: x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt1.Y; break; case XLinearGradientMode.Vertical: x1 = pt1.X; y1 = pt1.Y; x2 = pt1.X; y2 = pt2.Y; break; case XLinearGradientMode.ForwardDiagonal: x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt2.Y; break; case XLinearGradientMode.BackwardDiagonal: x1 = pt2.X; y1 = pt1.Y; x2 = pt1.X; y2 = pt2.Y; break; } } else { XPoint pt1 = renderer.WorldToView(brush._point1); XPoint pt2 = renderer.WorldToView(brush._point2); x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt2.Y; } const string format = Config.SignificantFigures3; Elements[Keys.Coords] = new PDFLiteral("[{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}]", x1, y1, x2, y2); //Elements[Keys.Background] = new PDFRawItem("[0 1 1]"); //Elements[Keys.Domain] = Elements[Keys.Function] = function; //Elements[Keys.Extend] = new PDFRawItem("[true true]"); string clr1 = "[" + PDFEncoders.ToString(color1, colorMode) + "]"; string clr2 = "[" + PDFEncoders.ToString(color2, colorMode) + "]"; function.Elements["/FunctionType"] = new PDFInteger(2); function.Elements["/C0"] = new PDFLiteral(clr1); function.Elements["/C1"] = new PDFLiteral(clr2); function.Elements["/Domain"] = new PDFLiteral("[0 1]"); function.Elements["/N"] = new PDFInteger(1); }