JD2022-TU1/main/tools/framework/JD.Application.TeaBox/View/WindowLess/WindowlessExtensions.cs

97 lines
No EOL
3.6 KiB
C#

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