Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DrawingExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f98529c76566a70aec8e605127acb1c6436df684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Drawing;

namespace UVtools.Core.Extensions
{
    public static class DrawingExtensions
    {
        public static Color FactorColor(this Color color, byte pixelColor, byte min = 0, byte max = byte.MaxValue) =>
            FactorColor(color, pixelColor / 255f, min, max);

        public static Color FactorColor(this Color color, float factor, byte min = 0, byte max = byte.MaxValue)
        {
            byte r = (byte)(color.R == 0 ? 0 :
                Math.Min(Math.Max(min, color.R * factor), max));

            byte g = (byte)(color.G == 0 ? 0 :
                Math.Min(Math.Max(min, color.G * factor), max));

            byte b = (byte)(color.B == 0 ? 0 :
                Math.Min(Math.Max(min, color.B * factor), max));
            return Color.FromArgb(r, g, b);
        }
    }
}