private void Initialize(byte red, byte green, byte blue, byte alpha) { R = Quantum.Convert(red); G = Quantum.Convert(green); B = Quantum.Convert(blue); A = Quantum.Convert(alpha); K = 0; }
/// <summary> /// Updates the color value in an inherited class. /// </summary> protected override void UpdateColor() { QuantumType gray = Quantum.ScaleToQuantum(_Shade); Color.R = gray; Color.G = gray; Color.B = gray; }
private ColorGray(IMagickColor <QuantumType> color) : base(color) { _shade = (0.212656 * Quantum.ScaleToDouble(color.R)) + (0.715158 * Quantum.ScaleToDouble(color.G)) + (0.072186 * Quantum.ScaleToDouble(color.B)); }
/// <summary> /// Initializes the color with the specified bytes. /// </summary> /// <param name="red">Red component value of this color.</param> /// <param name="green">Green component value of this color.</param> /// <param name="blue">Blue component value of this color.</param> /// <param name="alpha">Alpha component value of this color.</param> public void SetFromBytes(byte red, byte green, byte blue, byte alpha) { R = Quantum.Convert(red); G = Quantum.Convert(green); B = Quantum.Convert(blue); A = Quantum.Convert(alpha); K = 0; IsCmyk = false; }
/// <summary> /// Updates the color value in an inherited class. /// </summary> protected override void UpdateColor() { if (Math.Abs(Saturation) < double.Epsilon) { Color.R = Color.G = Color.B = Quantum.ScaleToQuantum(Value); return; } double h = 6.0 * (Hue - Math.Floor(Hue)); double f = h - Math.Floor(h); double p = Value * (1.0 - Saturation); double q = Value * (1.0 - (Saturation * f)); double t = Value * (1.0 - (Saturation * (1.0 - f))); switch ((int)h) { case 0: default: Color.R = Quantum.ScaleToQuantum(Value); Color.G = Quantum.ScaleToQuantum(t); Color.B = Quantum.ScaleToQuantum(p); break; case 1: Color.R = Quantum.ScaleToQuantum(q); Color.G = Quantum.ScaleToQuantum(Value); Color.B = Quantum.ScaleToQuantum(p); break; case 2: Color.R = Quantum.ScaleToQuantum(p); Color.G = Quantum.ScaleToQuantum(Value); Color.B = Quantum.ScaleToQuantum(t); break; case 3: Color.R = Quantum.ScaleToQuantum(p); Color.G = Quantum.ScaleToQuantum(q); Color.B = Quantum.ScaleToQuantum(Value); break; case 4: Color.R = Quantum.ScaleToQuantum(t); Color.G = Quantum.ScaleToQuantum(p); Color.B = Quantum.ScaleToQuantum(Value); break; case 5: Color.R = Quantum.ScaleToQuantum(Value); Color.G = Quantum.ScaleToQuantum(p); Color.B = Quantum.ScaleToQuantum(q); break; } }
private static IEnumerable <QuantumType> ParseQ8(string value) { byte red; byte green; byte blue; byte alpha; if (value.Length == 3) { yield return(Quantum.Convert((byte)ParseHex(value, 1, 2))); } else if (value.Length == 4 || value.Length == 5) { red = (byte)ParseHex(value, 1, 1); red += (byte)(red * 16); yield return(Quantum.Convert(red)); green = (byte)ParseHex(value, 2, 1); green += (byte)(green * 16); yield return(Quantum.Convert(green)); blue = (byte)ParseHex(value, 3, 1); blue += (byte)(blue * 16); yield return(Quantum.Convert(blue)); if (value.Length == 5) { alpha = (byte)ParseHex(value, 4, 1); alpha += (byte)(alpha * 16); yield return(Quantum.Convert(alpha)); } } else if (value.Length == 7 || value.Length == 9) { red = (byte)ParseHex(value, 1, 2); yield return(Quantum.Convert(red)); green = (byte)ParseHex(value, 3, 2); yield return(Quantum.Convert(green)); blue = (byte)ParseHex(value, 5, 2); yield return(Quantum.Convert(blue)); if (value.Length == 9) { alpha = (byte)ParseHex(value, 7, 2); yield return(Quantum.Convert(alpha)); } } else { throw new ArgumentException("Invalid hex value.", nameof(value)); } }
/// <summary> /// Converts the value of this instance to a <see cref="byte"/> array (RGBA or CMYKA). /// </summary> /// <returns>The <see cref="byte"/> array.</returns> public byte[] ToByteArray() { if (IsCmyk) { return new[] { Quantum.ScaleToByte(R), Quantum.ScaleToByte(G), Quantum.ScaleToByte(B), Quantum.ScaleToByte(K), Quantum.ScaleToByte(A) } } ; else { return new[] { Quantum.ScaleToByte(R), Quantum.ScaleToByte(G), Quantum.ScaleToByte(B), Quantum.ScaleToByte(A) } }; }
/// <summary> /// Converts the value of this instance to an equivalent Color. /// </summary> /// <returns>A <see cref="Color"/> instance.</returns> public Color ToColor() { if (!_isCmyk) { return(Color.FromArgb(Quantum.ScaleToByte(A), Quantum.ScaleToByte(R), Quantum.ScaleToByte(G), Quantum.ScaleToByte(B))); } var r = Quantum.ScaleToQuantum(Quantum.Max - ((Quantum.ScaleToDouble(R) * (Quantum.Max - K)) + K)); var g = Quantum.ScaleToQuantum(Quantum.Max - ((Quantum.ScaleToDouble(G) * (Quantum.Max - K)) + K)); var b = Quantum.ScaleToQuantum(Quantum.Max - ((Quantum.ScaleToDouble(B) * (Quantum.Max - K)) + K)); return(Color.FromArgb(Quantum.ScaleToByte(A), Quantum.ScaleToByte(r), Quantum.ScaleToByte(g), Quantum.ScaleToByte(b))); }
private void ParseQ8HexColor(string color) { byte red; byte green; byte blue; byte alpha = 255; if (color.Length == 3) { red = green = blue = (byte)ParseHex(color, 1, 2); } else if (color.Length == 4 || color.Length == 5) { red = (byte)ParseHex(color, 1, 1); red += (byte)(red * 16); green = (byte)ParseHex(color, 2, 1); green += (byte)(green * 16); blue = (byte)ParseHex(color, 3, 1); blue += (byte)(blue * 16); if (color.Length == 5) { alpha = (byte)ParseHex(color, 4, 1); alpha += (byte)(alpha * 16); } } else if (color.Length == 7 || color.Length == 9) { red = (byte)ParseHex(color, 1, 2); green = (byte)ParseHex(color, 3, 2); blue = (byte)ParseHex(color, 5, 2); if (color.Length == 9) { alpha = (byte)ParseHex(color, 7, 2); } } else { throw new ArgumentException("Invalid hex value."); } Initialize(Quantum.Convert(red), Quantum.Convert(green), Quantum.Convert(blue), Quantum.Convert(alpha)); }
private static bool TryParseQ16(string value, List <QuantumType> channels) { if (value.Length != 13 && value.Length != 17) { return(false); } for (int i = 1; i < value.Length; i += 4) { if (!TryParseHex(value, i, 4, out ushort channel)) { return(false); } channels.Add(Quantum.Convert(channel)); } return(true); }
/// <summary> /// Converts the value of this instance to a hexadecimal string that will not include the alpha channel if it is opaque. /// </summary> /// <returns>The <see cref="string"/>.</returns> public string ToHexString() { if (IsCmyk) { throw new NotSupportedException("This method only works for non cmyk colors."); } var r = Quantum.ScaleToByte(R); var g = Quantum.ScaleToByte(G); var b = Quantum.ScaleToByte(G); if (A == Quantum.Max) { return(string.Format(CultureInfo.InvariantCulture, "#{0:X2}{1:X2}{2:X2}", r, g, b)); } var a = Quantum.ScaleToByte(G); return(string.Format(CultureInfo.InvariantCulture, "#{0:X2}{1:X2}{2:X2}{3:X2}", r, g, b, a)); }
/// <summary> /// Updates the color value in an inherited class. /// </summary> protected override void UpdateColor() { Color.R = Quantum.ScaleToQuantum(Y - (3.945707070708279e-05 * (U - 0.5)) + (1.1398279671717170825 * (V - 0.5))); Color.G = Quantum.ScaleToQuantum(Y - (0.3946101641414141437 * (U - 0.5)) - (0.5805003156565656797 * (V - 0.5))); Color.B = Quantum.ScaleToQuantum(Y + (2.0319996843434342537 * (U - 0.5)) - (4.813762626262513e-04 * (V - 0.5))); }
/// <summary> /// Converts the value of this instance to an equivalent Color. /// </summary> /// <returns>A <see cref="Color"/> instance.</returns> public Color ToColor() { return(Color.FromArgb(Quantum.ScaleToByte(A), Quantum.ScaleToByte(R), Quantum.ScaleToByte(G), Quantum.ScaleToByte(B))); }
private ColorGray(MagickColor color) : base(color) { _shade = Quantum.ScaleToQuantum(color.R); }
private ColorGray(IMagickColor <QuantumType> color) : base(color) { _shade = Quantum.ScaleToQuantum(color.R); }
/// <summary> /// Updates the color value in an inherited class. /// </summary> protected override void UpdateValue() { Value.R = Quantum.ScaleToQuantum(_Y + (1.13980 * _V)); Value.G = Quantum.ScaleToQuantum(_Y - (0.39380 * _U) - (0.58050 * _V)); Value.B = Quantum.ScaleToQuantum(_Y + (2.02790 * _U)); }