using System; using System.Collections.Generic; using System.Drawing; using JD.Kernel.Classes; using JD.Kernel.Structs; using JD.Sequencers.Windowlesses; using TeaBox; namespace JD.Application.View.WindowLess { /// /// Extension methods to help manipulate write painting algorithm. /// public static class WindowlessExtensions { /// /// Enumerates children of the control recursively. /// public static IEnumerable ControlsRecursive(this WindowlessControl control) { foreach (var child in control.Controls) { yield return child; foreach (var grandChild in child.ControlsRecursive()) { yield return grandChild; } } } /// /// Positions the graphics in the local coordinates during the scope. Returns the graphis to /// its original value once the user disposes the returned handle. /// public static IDisposable LocalDraw(this Graphics graphics, WindowlessControl owner) { var container = graphics.UsingContainer(); graphics.TranslateTransform(owner.Bounds.Left, owner.Bounds.Top); return container; } /// /// Allows to modify the graphics during a given scope. /// The graphics returns to its original value once the user disposes the returned handle. /// public static IDisposable UsingContainer(this Graphics graphics) { var container = graphics.BeginContainer(); return new DisposableHelper(() => graphics.EndContainer(container)); } /// /// Adds the requested to each color component allowing to lighten or darken the current color. /// public static Color Add(this Color source, int delta) { var colorRange = new Range(0, 255); Func modifyChannel = x => colorRange.Clamp(x + delta); return Color.FromArgb(source.A, modifyChannel(source.R), modifyChannel(source.G), modifyChannel(source.B)); } /// /// Modulates from source to target using the /// /// /// /// 0 means source, 1 means target public static Color ModulateWith(this Color source, Color toApply, float factor) { return Color.FromArgb(source.A, Lerp(source.R, toApply.R, factor), Lerp(source.G, toApply.G, factor), Lerp(source.B, toApply.B, factor)); } private static int Lerp(int a, int b, float factor) { var colorRange = new Range(0, 255); return colorRange.Clamp((int)(a * (1-factor) + b * factor)); } /// /// Returns a color with the alpha channel modified. /// public static Color WithAlpha(this Color source, int alpha) { return Color.FromArgb(alpha, source); } /// /// Returns true if the color is considered to be bright /// public static bool IsBright(this Color source) { return (source.R + source.G + source.B) > 448; } } }