-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
66 lines (57 loc) · 1.85 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using UnityEngine;
namespace KS.UnityDarken
{
public static class Utils
{
public static Color InvertColor( Color color )
//tried also hsv inversion but this looks better
{
return new Color( 1 - color.r, 1 - color.g, 1 - color.b, color.a );
}
public static Color InvertColorWithAddition( Color color, ColorWithRange[] colorsPalette )
//tried also hsv inversion but this looks better
{
var r = color.r;
var g = color.g;
var b = color.b;
var brightness = 0.2126f*r + 0.7152f*g + 0.0722f*b;
Color additive = Color.black;
foreach ( var col in colorsPalette )
{
if ( brightness > col.BrightnessRangeStart
&& brightness < col.BrightnessRangeEnd )
{
var normalized = (brightness - col.BrightnessRangeStart)
/ (col.BrightnessRangeEnd - col.BrightnessRangeStart);
additive += col.ColorToAdd.Evaluate( normalized ) * col.Multiplier;
}
}
return new Color(
Mathf.Clamp01( 1 - r + additive.r ),
Mathf.Clamp01( 1 - g + additive.g ),
Mathf.Clamp01( 1 - b + additive.b ), color.a );
}
//kinda impossible to reverse it, have to store additive color somewhere
public static Color ReverseInvertColorWithAddition( Color color, ColorWithRange[] colorsPalette )
//tried also hsv inversion but this looks better
{
//var color = new Color( 1 - invertedColor.r, 1 - invertedColor.g, 1 - invertedColor.b, invertedColor.a );
var r = color.r;
var g = color.g;
var b = color.b;
//var brightness = 1 - (0.2126f*r + 0.7152f*g + 0.0722f*b);
//Color additive = Color.black;
//foreach ( var col in colorsPalette )
//{
// if ( brightness > col.BrightnessRangeStart
// && brightness < col.BrightnessRangeEnd )
// {
// additive += col.ColorToAdd.Evaluate( 0.5f );
// }
//}
//additive.a = 0;
//color -= additive;
return new Color( 1 - r, 1 - g, 1 - b, color.a );
}
}
}