/// <summary> /// Blends two colours together. /// </summary> /// <param name="c1">The first colour to blend.</param> /// <param name="c2">The second colour to blend.</param> /// <param name="middle">How many colours to create.</param> /// <returns>A list of the colours blended.</returns> public static IEnumerable <RgbColor> BlendColours(RgbColor c1, RgbColor c2, int middle) { var steps = middle + 1; // intermediate color var sr = (c2.R - c1.R) / steps; var sg = (c2.G - c1.G) / steps; var sb = (c2.B - c1.B) / steps; for (int i = 1; i < steps; i++) { yield return(new RgbColor( c1.R + (sr * i), c1.G + (sg * i), c1.B + (sb * i), 16)); } }
/// <summary> /// Creates a new color for use in a palette. /// </summary> /// <param name="l"> /// The color in a text based form, such as "0 0 0[tab]Untitled". This /// format is used by <see cref="GimpPalette"/>. /// </param> public PaletteColor(string l) { var groups = Regex.Match(l, matchRegex).Groups; if (groups.Count < 4) { throw new PaletteException("The string was invalid."); } var r = byte.Parse(groups[1].Value); var g = byte.Parse(groups[2].Value); var b = byte.Parse(groups[3].Value); Color = new RgbColor(r, g, b); Name = String.IsNullOrEmpty(groups[4]?.Value) ? "Untitled" : groups[4].Value; }
public static Color ToGdiColor(this RgbColor c) { return(Color.FromArgb(c.R8, c.G8, c.B8)); }
/// <summary> /// Creates a palette from a Photoshop color book file. /// </summary> /// <param name="file">The file to convert from.</param> public AcbPalette(byte[] file) : this() { using (var ms = new MemoryStream(file)) { using (var sr = new BinaryReader(ms)) { if (new string(sr.ReadChars(4)) != magic) { throw new PaletteException("Not a valid color book."); } var version = sr.ReadUInt16BE(); ID = sr.ReadUInt16BE(); Name = UnescapeString(GetValue(sr.ReadStringBE(false))); Prefix = GetValue(sr.ReadStringBE(false)); Postfix = GetValue(sr.ReadStringBE(false)); Description = UnescapeString(GetValue(sr.ReadStringBE(false))); var count = sr.ReadUInt16BE(); BucketSize = sr.ReadUInt16BE(); DefaultSelection = sr.ReadUInt16BE(); ColorSpace = (AdobeColorSpaceAcbSubset)sr.ReadUInt16BE(); for (int i = 0; i < count; i++) { var name = sr.ReadStringBE(true); // TODO: this needs to be associated with the PaletteColor var cid = new string(sr.ReadChars(6)); #if DEBUG System.Diagnostics.Debug.WriteLine("{0}{1}{2}:{3}", Prefix, name, Postfix, cid); #endif IColor color; switch (ColorSpace) { case AdobeColorSpaceAcbSubset.Rgb: color = new RgbColor(sr.ReadByte(), sr.ReadByte(), sr.ReadByte()); break; // For lab and cmyk, should we add 0.5? // (seems to be not a good idea) case AdobeColorSpaceAcbSubset.Lab: var l = sr.ReadByte() / 2.55d; var a = sr.ReadByte() - 128; var b = sr.ReadByte() - 128; color = new LabColor(l, a, b); break; case AdobeColorSpaceAcbSubset.Cmyk: var c = 1 - sr.ReadByte() / 255d; var m = 1 - sr.ReadByte() / 255d; var y = 1 - sr.ReadByte() / 255d; var k = 1 - sr.ReadByte() / 255d; color = new CmykColor(c, m, y, k); break; default: throw new PaletteException(string.Format( "Invalid colorspace. {0}", ColorSpace)); } Colors.Add(new PaletteColor(color, name, cid)); } if (ms.Length > ms.Position) { var purpose = new string(sr.ReadChars(8)); switch (purpose) { case spot: Purpose = AcbPurpose.Spot; break; case proc: Purpose = AcbPurpose.Process; break; default: Purpose = AcbPurpose.Unknown; break; } } } } }
public static uint ToGdkPixel(this RgbColor c) { // HACK: Gdk.Color.Pixel doesn't work well, even // with colormap alloc return((uint)((c.R8 << 24) | (c.G8 << 16) | (c.B8 << 8) | 0xFF)); }
PaletteColor() { Color = new RgbColor(); Name = "Untitled"; Metadata = string.Empty; }
public ColorGridChangeEventArgs(PaletteColor old, RgbColor @new) { Color = old; NewColor = @new; }
public BlendDialog(RgbColor c1, RgbColor c2) : this() { colorbutton1.Color = c1.ToGdkColor(); colorbutton2.Color = c2.ToGdkColor(); UpdateUI(); }
public BlendForm(RgbColor c1, RgbColor c2) : this() { colorButton1.Color = c1; colorButton2.Color = c2; UpdateUI(); }
/// <summary> /// Renders an icon, representing a square of the specified color with a black border. /// </summary> /// <param name="c">The color to use.</param> /// <param name="w">The width of the icon. By default, 16.</param> /// <param name="h">The height of the icon. By default, 16.</param> /// <returns>The icon.</returns> public static Icon RenderIcon(RgbColor c, int w = 16, int h = 16) { return(RenderIcon(c.ToGdiColor(), w, h)); }
/// <summary> /// Renders a bitmap, representing a square of the specified color with a black border. /// </summary> /// <param name="c">The color to use.</param> /// <param name="w">The width of the bitmap. By default, 16.</param> /// <param name="h">The height of the bitmap. By default, 16.</param> /// <returns>The bitmap.</returns> public static Bitmap RenderBitmap(RgbColor c, int w = 16, int h = 16) { return(RenderBitmap(c.ToGdiColor(), w, h)); }
/// <summary> /// Creates a palette from an Adobe Swatch Exchange file. /// </summary> /// <param name="file">The file, as an array of bytes.</param> /// <returns>The converted palette.</returns> public AsePalette(byte[] file) : this() { using (var ms = new MemoryStream(file)) { using (var sr = new BinaryReader(ms)) { if (new string(sr.ReadChars(4)) != "ASEF") { throw new PaletteException("Not an ASE file."); } // major, minor var version = new Version(sr.ReadUInt16BE(), sr.ReadUInt16BE()); // should we bomb if it's not a version we accept? if (version > new Version(1, 0)) { throw new PaletteException("The version is unsupported."); } var count = sr.ReadUInt32BE(); for (int i = 0; i < count; i++) { var type = sr.ReadUInt16BE(); var length = sr.ReadUInt32BE(); if (type == 1) // color entry { var nameLen = sr.ReadUInt16BE(); var name = sr.ReadStringBE(nameLen, true); var colorSpace = new string(sr.ReadChars(4)); IColor color; switch (colorSpace) { case "RGB ": var r = sr.ReadSingleBE(); var g = sr.ReadSingleBE(); // HACK: we can't use same named variable // declarations in a switch/case block, due // to C behaviour leftovers var blue = sr.ReadSingleBE(); var rr = Convert.ToUInt16(Math.Round(r * 65535)); var gr = Convert.ToUInt16(Math.Round(g * 65535)); var br = Convert.ToUInt16(Math.Round(blue * 65535)); color = new RgbColor(rr, gr, br); break; case "LAB ": var l = sr.ReadSingleBE() * 100; var a = sr.ReadSingleBE(); var b = sr.ReadSingleBE(); color = new LabColor(l, a, b); break; default: throw new PaletteException( string.Format("The colorspace ({0}) is unsupported.", colorSpace)); } Colors.Add(new PaletteColor(color, name)); var colorType = sr.ReadUInt16BE(); } else { // skip ahead sr.ReadBytes(Convert.ToInt32(length)); } } } } }