ColorSpectrum.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample shows how a color spectrum with fall offs from fully saturated
  20. // colors to white and black can be easily created using linear gradient brushes
  21. // and GcBitmap.Renderer.TransparencyMaskBitmap.
  22. public class ColorSpectrum
  23. {
  24. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  25. {
  26. // Color spectrum gradient brush:
  27. var spec = new LinearGradientBrush(Color.Red, Color.Red);
  28. var step = 1f / 6f;
  29. spec.GradientStops = new GradientStop[]
  30. {
  31. new GradientStop(Color.Yellow, step),
  32. new GradientStop(Color.Green, step * 2),
  33. new GradientStop(Color.Cyan, step * 3),
  34. new GradientStop(Color.Blue, step * 4),
  35. new GradientStop(Color.Magenta, step * 5),
  36. };
  37.  
  38. // Create the spectrum in a separate temp bitmap:
  39. var spectrumSz = new Size(pixelSize.Width, (int)(pixelSize.Height * 0.75f));
  40. // Falloff brush and mask:
  41. using var tbmp = new GcBitmap(spectrumSz.Width, spectrumSz.Height, true);
  42. using var tgfx = tbmp.CreateGraphics();
  43. // Black is opaque, White transparent:
  44. var falloff = new LinearGradientBrush(Color.Black, PointF.Empty, Color.Black, new PointF(0, 1));
  45. // Additional gray stops provide a more gradual and pleasing appearance:
  46. falloff.GradientStops = new GradientStop[]
  47. {
  48. new GradientStop(Color.LightGray, 0.35f),
  49. new GradientStop(Color.White, 0.5f),
  50. new GradientStop(Color.LightGray, 0.65f)
  51. };
  52. // Fill the mask with the gradient that will define the transparency:
  53. tgfx.FillRectangle(new RectangleF(0, 0, tbmp.Width, tbmp.Height), falloff);
  54. // Convert mask to grayscale for use as the transparency mask:
  55. using var mask = tbmp.ToGrayscaleBitmap();
  56. // Reuse the original mask bitmap;
  57. // Fill it with a gradient from white at the top to black at the bottom:
  58. var grad = new LinearGradientBrush(Color.White, PointF.Empty, Color.Black, new PointF(0, 1));
  59. tgfx.FillRectangle(new RectangleF(0, 0, spectrumSz.Width, spectrumSz.Height), grad);
  60.  
  61. // Add transparency mask and draw the gradient:
  62. tbmp.Renderer.TransparencyMaskBitmap = mask;
  63. tgfx.FillRectangle(new RectangleF(PointF.Empty, spectrumSz), spec);
  64.  
  65. // Copy spectrum to the target bitmap:
  66. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  67. bmp.BitBlt(tbmp, 0, 0);
  68.  
  69. // Add some explanatory texts below the spectrum:
  70. var g = bmp.CreateGraphics();
  71. var rc = new RectangleF(0, spectrumSz.Height, spectrumSz.Width, bmp.PixelHeight - spectrumSz.Height);
  72. var tf = new TextFormat()
  73. {
  74. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "courbd.ttf")),
  75. FontSize = 14,
  76. };
  77. g.FillRectangle(rc, Color.LightGray);
  78. g.DrawString(
  79. "The color spectrum is drawn using a horizontal linear gradient brush with 7 stops:\n" +
  80. "Red - Yellow - Green - Cyan - Blue - Magenta - Red.\n" +
  81. "The background is filled with a vertical gradient from white to black. " +
  82. "A TransparencyMaskBitmap with a vertical gradient that changes from 0 " +
  83. "(opaque) to 1 (transparent) back to 0 (opaque) provides falloffs " +
  84. "from spectrum colors in the center up to white and down to black backgrounds.",
  85. tf, rc, TextAlignment.Center, ParagraphAlignment.Center);
  86.  
  87. return bmp;
  88. }
  89. }
  90. }
  91.