public static Texture2D GetGradient(int sR, int sG, int sB, int sA, int eR, int eG, int eB, int eA, GradientSide gradientSide, int steps) { return(GetGradient(new Color(sR / 255f, sG / 255f, sB / 255f, sA / 255f), new Color(eR / 255f, eG / 255f, eB / 255f, eA / 255f), gradientSide, steps)); }
public static Texture2D GetGradient(Color startColor, Color endColor, GradientSide gradientSide, int steps) { Object ret; string name = "_g_" + startColor.r + "_" + startColor.g + "_" + startColor.b + "_" + startColor.a + "_" + endColor.r + "_" + endColor.g + "_" + endColor.b + "_" + endColor.a + "_" + gradientSide + "_" + steps; if (objects.TryGetValue(name, out ret)) { return((Texture2D)ret); } Texture2D tex = new Texture2D(gradientSide == GradientSide.LEFT || gradientSide == GradientSide.RIGHT ? steps : 1, gradientSide == GradientSide.TOP || gradientSide == GradientSide.BOTTOM ? steps : 1); Color[] colors = CalculateGradient(startColor, endColor, steps); if (gradientSide == GradientSide.TOP || gradientSide == GradientSide.BOTTOM) { for (int i = 0; i < colors.Length; i++) { tex.SetPixel(0, gradientSide == GradientSide.TOP ? i : colors.Length - i - 1, colors[i]); } } else { for (int i = 0; i < colors.Length; i++) { tex.SetPixel(gradientSide == GradientSide.LEFT ? i : colors.Length - i - 1, 0, colors[i]); } } tex.Apply(); objects.Add(name, tex); return(tex); }
public static bool AlphaGradient(ref Bitmap bmp, float[] factor, float[] position, GradientSide gs) { if (bmp.PixelFormat != PixelFormat.Format24bppRgb) return false; int w = bmp.Width; int h = bmp.Height; Bitmap tmp = new Bitmap(w, h, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(tmp); g.DrawImageUnscaled(bmp, 0, 0); g.Dispose(); Color startColor = Color.White; Color endColor = Color.Black; Point start = new Point(-1, 0); Point end = new Point(0, 0); switch (gs) { case GradientSide.Left: end.X = w; break; case GradientSide.Right: start.X = w; break; case GradientSide.Upper: end.Y = h; break; case GradientSide.Lower: start.Y = h; break; case GradientSide.UpperLeft: end.X = w; end.Y = h; break; case GradientSide.UpperRight: start.X = w; end.Y = h; break; case GradientSide.LowerLeft: start.Y = h; end.X = w; break; case GradientSide.LowerRight: start.X = w; start.Y = h; break; } Blend bl = new Blend(); bl.Factors = factor; bl.Positions = position; LinearGradientBrush br = new LinearGradientBrush( start, end, startColor, endColor); br.Blend = bl; //br.GammaCorrection = true; Rectangle rct = new Rectangle(0, 0, w, h); g = Graphics.FromImage(bmp); g.FillRectangle(br, rct); g.Dispose(); BmpProc24 src = new BmpProc24(bmp); BmpProc32 dst = new BmpProc32(tmp); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) dst[x, y, eRGB.a] = src[x, y, eRGB.r]; CallDispose(dst, src, bmp); bmp = tmp; return true; }
public static bool GradientColor(ref Bitmap bmp, float[] factor, float[] position, GradientSide gs) { Bitmap tmp = bmp.Clone() as Bitmap; if (!AlphaGradient(ref tmp, factor, position, gs)) { tmp.Dispose(); return false; } GrayScale24(ref bmp); Graphics g = Graphics.FromImage(bmp); g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return true; }