public ColorProperties(RGBAColor Color1, RGBAColor Color2, double Alpha, Vector2D GradientRotation, double GradientPosition) { this.Color1 = Color1; this.Color2 = Color2; this.Alpha = Alpha; this.GradientRotation = GradientRotation; this.GradientPosition = GradientPosition; }
/// <summary> /// Function to calculate the complementary color /// Note that the ! operator of RGBAColor does the same /// </summary> /// <param name="Col">Input color</param> /// <returns>Complement color of the RGB channels of the input color</returns> public static RGBAColor Complement(RGBAColor Col) { Col.R = 1 - Col.R; Col.G = 1 - Col.G; Col.B = 1 - Col.B; return Col; }
public SmoothLine(int framecount, int smoother, RGBAColor col, int lineWidth, int pointsrange) { FFRameCount = framecount; FSmoother = smoother; ColorOut = col; LineWidth = lineWidth; PointsRange = pointsrange; }
/// <summary> /// Adds a value to the RGB channels of a color and takes the result modulo 1 /// </summary> /// <param name="Col"></param> /// <param name="Offset"></param> /// <returns>(Col.RGB + Offset) modulo 1</returns> public static RGBAColor Offset(RGBAColor Col, double Offset) { Col.R = (Col.R + Offset) % 1.0; Col.G = (Col.G + Offset) % 1.0; Col.B = (Col.B + Offset) % 1.0; return Col; }
/// <summary> /// Function to get black or white, which ever has higher contrast to the input color, e.g. for text on colored backgrounds /// </summary> /// <param name="C">Input color</param> /// <returns>Black or white in C# color format</returns> public static Color Invert(Color C) { RGBAColor col = new RGBAColor(C.R/255.0, C.R/255.0, C.R/255.0, 1); if (Brightness(col) > 0.5) return Color.White; else return Color.Black; }
/// <summary> /// Computes RGB values from HSL values, found on: /// http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm /// </summary> /// <param name="H">Hue</param> /// <param name="S">Saturation</param> /// <param name="L">Lightness</param> /// <param name="Red">Output parameter, gets filled with the red value</param> /// <param name="Green">Output parameter, gets filled with the green value</param> /// <param name="Blue">Output parameter, gets filled with the blue value</param> public static void HSLtoRGB(double H, double S, double L, out double Red, out double Green, out double Blue) { Red = Green = Blue = L; // default to gray double v; v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S); if (v > 0) { double m; double sv; int sextant; double fract, vsf, mid1, mid2; m = L + L - v; sv = (v - m) / v; H *= 6.0; sextant = (int)H; fract = H - sextant; vsf = v * sv * fract; mid1 = m + vsf; mid2 = v - vsf; switch (sextant) { case 0: Red = v; Green = mid1; Blue = m; break; case 1: Red = mid2; Green = v; Blue = m; break; case 2: Red = m; Green = v; Blue = mid1; break; case 3: Red = m; Green = mid2; Blue = v; break; case 4: Red = mid1; Green = m; Blue = v; break; case 5: Red = v; Green = m; Blue = mid2; break; } } }
/// <summary> /// 2d linear interpolation in x and y direction for colors /// </summary> /// <param name="x">The x position where to interpolate, 0..1</param> /// <param name="y">The y position where to interpolate, 0..1</param> /// <param name="P1">Upper left color</param> /// <param name="P2">Upper right color</param> /// <param name="P3">Lower right color</param> /// <param name="P4">Lower left color</param> /// <returns>Interpolated color between the 4 colors in the corners</returns> public static RGBAColor BilerpRGBA(double x, double y, RGBAColor P1, RGBAColor P2, RGBAColor P3, RGBAColor P4) { //interpolate lower colors in x direction P1 = LerpRGBA(P1, P2, x); //interpolate upper colors in x direction P3 = LerpRGBA(P4, P3, x); //interpolate results in y direction return(LerpRGBA(P3, P1, y)); }
/// <summary> /// Calculates the brighness of a color with the formula 0.222 * R + 0.707 * G + 0.071 * B /// </summary> /// <param name="C"></param> /// <returns>Brightness value of the input color C</returns> public static double Brightness(RGBAColor C) { return(0.222 * C.R + 0.707 * C.G + 0.071 * C.B); }
public bool Process(List<string> addressList, ArrayList values, Matrix4x4 transform) { this.Address = addressList[0]; var localAddress = new List<string>(addressList); localAddress.RemoveAt(0); if (localAddress.Count > 0) { switch (localAddress[0]) { case "begin": this.inputBuffer = new Spline(false); this.inputBuffer.Address = this.Address; break; case "end": this.Position = new Vector3D(this.inputBuffer.Position); this.Vertices = new List<Vector3D>(this.inputBuffer.Vertices); this.Color = new RGBAColor(); this.Color.R = this.inputBuffer.Color.R; this.Color.G = this.inputBuffer.Color.G; this.Color.B = this.inputBuffer.Color.B; this.Color.A = this.inputBuffer.Color.A; this.UserData = new List<UserDataEntry>(inputBuffer.UserData); this.Children = new Dictionary<string,Spline>(this.inputBuffer.Children); return true; case "position": if (values.Count >= 3) { for(int i=0; i<3; i++) { this.inputBuffer.Position[i] = (double) (float) values[i]; } if (transform != null) { this.inputBuffer.Position = transform * this.inputBuffer.Position; } } break; case "displayColor": if (values.Count >= 3) { this.inputBuffer.Color.R = (double) (float) values[0]; this.inputBuffer.Color.G = (double) (float) values[1]; this.inputBuffer.Color.B = (double) (float) values[2]; } break; case "childCount": break; case "userData": localAddress.RemoveAt(0); string propertyName = ""; foreach (var level in localAddress) { if (propertyName != "") { propertyName += "/"; } propertyName += level; } var userDataEntry = new UserDataEntry(); userDataEntry.Name = propertyName; foreach (var value in values) { if (value is int) { userDataEntry.Values.Add((double)(int)value); } else if (value is float) { userDataEntry.Values.Add((double)(float)value); } } this.inputBuffer.UserData.Add(userDataEntry); break; case "spline": this.inputBuffer.Vertices.Clear(); for (int i = 0; i < values.Count / 3; i++) { var vertex = new Vector3D(); for(int j=0; j<3; j++) { vertex[j] = (double)(float)values[i * 3 + j]; } if (transform != null) { this.inputBuffer.Vertices.Add(transform * vertex); } else { this.inputBuffer.Vertices.Add(vertex); } } break; default: var childName = localAddress[0]; if (!inputBuffer.Children.ContainsKey(childName)) { inputBuffer.Children.Add(childName, new Spline(true)); } inputBuffer.Children[childName].Process(localAddress, values, transform); break; } } return false; }
/// <summary> /// Linear interpolation (blending) between two colors /// </summary> /// <param name="Col1"></param> /// <param name="Col2"></param> /// <param name="x">Blending factor, 0..1</param> /// <returns>Linear interpolation (blending) between Col1 and Col2 if x in the range ]0..1[, Col1 if x = 0, Col2 if x = 1</returns> public static RGBAColor LerpRGBA(RGBAColor Col1, RGBAColor Col2, double x) { return(Col1 + x * (Col2 - Col1)); }
/// <summary> /// Linear interpolation (blending) between two colors /// </summary> /// <param name="Col1"></param> /// <param name="Col2"></param> /// <param name="x">Blending factor, 0..1</param> /// <returns>Linear interpolation (blending) between Col1 and Col2 if x in the range ]0..1[, Col1 if x = 0, Col2 if x = 1</returns> public static RGBAColor LerpRGBA(RGBAColor Col1, RGBAColor Col2, double x) { return Col1 + x * (Col2 - Col1); }
public bool Equals(RGBAColor other) { return this.R == other.R && this.G == other.G && this.B == other.B && this.A == other.A; }
protected static byte[] packColor(RGBAColor value) { double[] rgba = { value.R, value.G, value.B, value.A }; byte[] data = new byte[rgba.Length]; for (int i = 0; i < rgba.Length; i++) data[i] = (byte)Math.Round(rgba[i] * 255); if (BitConverter.IsLittleEndian) data = swapEndian(data); return data; }
/// <summary> /// 2d linear interpolation in x and y direction for colors /// </summary> /// <param name="x">The x position where to interpolate, 0..1</param> /// <param name="y">The y position where to interpolate, 0..1</param> /// <param name="P1">Upper left color</param> /// <param name="P2">Upper right color</param> /// <param name="P3">Lower right color</param> /// <param name="P4">Lower left color</param> /// <returns>Interpolated color between the 4 colors in the corners</returns> public static RGBAColor BilerpRGBA(double x, double y, RGBAColor P1, RGBAColor P2, RGBAColor P3, RGBAColor P4) { //interpolate lower colors in x direction P1 = LerpRGBA(P1, P2, x); //interpolate upper colors in x direction P3 = LerpRGBA(P4, P3, x); //interpolate results in y direction return LerpRGBA(P3, P1, y); }
Bitmap ComputeQRCode(int slice) { var qrEncoder = new QrEncoder(FErrorCorrectionLevel[slice]); var qrCode = new QrCode(); if (qrEncoder.TryEncode(FText[slice], out qrCode)) { var fc = FForeColor[slice]; var bc = FBackColor[slice]; fc = new RGBAColor(fc.B, fc.G, fc.R, fc.A); bc = new RGBAColor(bc.B, bc.G, bc.R, bc.A); using (var fore = new SolidBrush(fc.Color)) using (var back = new SolidBrush(bc.Color)) { var renderer = new GraphicsRenderer(new FixedModuleSize(FPixelSize[slice], FQuietZoneModules[slice]), fore, back); DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width); var bmp = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); using (var g = Graphics.FromImage(bmp)) renderer.Draw(g, qrCode.Matrix); return bmp; } } else return null; }
/// <summary> /// Calculates the brighness of a color with the formula 0.222 * R + 0.707 * G + 0.071 * B /// </summary> /// <param name="C"></param> /// <returns>Brightness value of the input color C</returns> public static double Brightness(RGBAColor C) { return 0.222 * C.R + 0.707 * C.G + 0.071 * C.B; }
/// <summary> /// Converts a RGBAColor to a string with semicolon separator /// </summary> /// <param name="col"></param> /// <returns></returns> public static string Serialze(RGBAColor col) { return col.R.ToString("r", CultureInfo.InvariantCulture) + ";" + col.G.ToString("r", CultureInfo.InvariantCulture) + ";" + col.B.ToString("r", CultureInfo.InvariantCulture) + ";" + col.A.ToString("r", CultureInfo.InvariantCulture); }
/// <summary> /// Computes RGB values from HSL values, found on: /// http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm /// </summary> /// <param name="H">Hue</param> /// <param name="S">Saturation</param> /// <param name="L">Lightness</param> /// <param name="Red">Output parameter, gets filled with the red value</param> /// <param name="Green">Output parameter, gets filled with the green value</param> /// <param name="Blue">Output parameter, gets filled with the blue value</param> public static void HSLtoRGB(double H, double S, double L, out double Red, out double Green, out double Blue) { Red = Green = Blue = L; // default to gray double v; v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S); if (v > 0) { double m; double sv; int sextant; double fract, vsf, mid1, mid2; m = L + L - v; sv = (v - m ) / v; H *= 6.0; sextant = (int)H; fract = H - sextant; vsf = v * sv * fract; mid1 = m + vsf; mid2 = v - vsf; switch (sextant) { case 0: Red = v; Green = mid1; Blue = m; break; case 1: Red = mid2; Green = v; Blue = m; break; case 2: Red = m; Green = v; Blue = mid1; break; case 3: Red = m; Green = mid2; Blue = v; break; case 4: Red = mid1; Green = m; Blue = v; break; case 5: Red = v; Green = m; Blue = mid2; break; } } }
/// <summary> /// Function to convert HSV values to RGB values /// /// merged methods from EasyRGB (http://www.easyrgb.com/math.php?MATH=M21#text21) /// and the book GRAPHICS GEMS /// </summary> /// <param name="H"></param> /// <param name="S"></param> /// <param name="V"></param> /// <param name="Red">Output parameter, this variable gets filled with the red value</param> /// <param name="Green">Output parameter, this variable gets filled with the green value</param> /// <param name="Blue">Output parameter, this variable gets filled with the blue value</param> public static void HSVtoRGB (double H, double S, double V, out double Red, out double Green, out double Blue) { Red = Green = Blue = V; if (S != 0) { H = H - Math.Truncate(H); double min = V * (1 - S); H = 6 * H; int sextant = (int) Math.Truncate(H); double fract = H - sextant; double vsf = V * S * fract; double mid1 = min + vsf; double mid2 = V - vsf; switch (sextant) { case 0: {Red = V; Green = mid1; Blue = min; break;} case 1: {Red = mid2; Green = V; Blue = min; break;} case 2: {Red = min; Green = V; Blue = mid1; break;} case 3: {Red = min; Green = mid2; Blue = V; break;} case 4: {Red = mid1; Green = min; Blue = V; break;} case 5: {Red = V; Green = min; Blue = mid2; break;} } } }
public static Bin DeSerializeBin(this Stream input) { uint objCount = input.ReadUint(); uint aliasLength = input.ReadUint(); var alias = input.ReadUnicode((int) aliasLength); var type = TypeIdentity.Instance.FindType(alias); var bin = Bin.New(type); for (int i = 0; i < objCount; i++) { if (type == typeof(bool)) bin.Add(input.ReadBool()); if (type == typeof(int)) bin.Add(input.ReadInt()); if (type == typeof(float)) bin.Add(input.ReadFloat()); if (type == typeof(double)) bin.Add(input.ReadDouble()); if (type == typeof(string)) { uint l = input.ReadUint(); if (l > 0) bin.Add(input.ReadUnicode((int)l)); else bin.Add(""); } if (type == typeof(RGBAColor)) { double[] val = new double[4]; for (int j = 0; j < val.Length; j++) val[j] = input.ReadDouble(); RGBAColor res = new RGBAColor(val); bin.Add(res); } if (type == typeof(Vector2D)) { Vector2D res = new Vector2D(); res.x = input.ReadDouble(); res.y = input.ReadDouble(); bin.Add(res); } if (type == typeof(Vector3D)) { Vector3D res = new Vector3D(); res.x = input.ReadDouble(); res.y = input.ReadDouble(); res.z = input.ReadDouble(); bin.Add(res); } if (type == typeof(Vector4D)) { Vector4D res = new Vector4D(); res.x = input.ReadDouble(); res.y = input.ReadDouble(); res.z = input.ReadDouble(); res.w = input.ReadDouble(); bin.Add(res); } if (type == typeof(Matrix4x4)) { Matrix4x4 res = new Matrix4x4(); for (int j = 0; j < 16; j++) res.Values[j] = input.ReadDouble(); bin.Add(res); } if (type == typeof(Stream)) { Stream res = new MemoryStream(); uint l = input.ReadUint(); if (l > 0) { input.CopyTo(res, (int) l); res.Position = 0; } bin.Add(res); } if (type == typeof(Time.Time)) { uint l = input.ReadUint(); var t =input.ReadUnicode((int)l); var utcTime = Time.Time.StringAsTime("UTC", t, "yyyy-MM-dd HH:mm:ss.ffff"); l = input.ReadUint(); var z = input.ReadUnicode((int)l); var timestamp = Time.Time.ChangeTimezone(utcTime, z); bin.Add(timestamp); } } return bin; }
//Output color public void SetColor(RGBAColor col) { ColorOut = col; }
protected static RGBAColor unpackColor(byte[] bytes, ref int start) { byte[] data = new byte[4]; for (int i = 0; i < 4; i++, start++) data[i] = bytes[start]; if (BitConverter.IsLittleEndian) data = swapEndian(data); var col = new RGBAColor(); col.R = (double)data[0] / 255.0; col.G = (double)data[1] / 255.0; col.B = (double)data[2] / 255.0; col.A = (double)data[3] / 255.0; return col; }
public bool Equals(RGBAColor other) { return(this.R == other.R && this.G == other.G && this.B == other.B && this.A == other.A); }