forked from YuriDLL/stacklands-data-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorUtility.cs
More file actions
75 lines (59 loc) · 2.2 KB
/
ColorUtility.cs
File metadata and controls
75 lines (59 loc) · 2.2 KB
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
67
68
69
70
71
72
73
74
75
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using d = DataLoaderPlugin.Dto;
namespace DataLoaderPlugin
{
public class ColorUtility
{
private static FieldInfo PropBlockFieldInfo = AccessTools.Field(typeof(GameCard), "propBlock");
private static MethodInfo SetColorMethodInfo = AccessTools.Method(typeof(GameCard), "SetColors");
public WorldManager WorldManager { get; }
public ColorUtility(WorldManager worldManager)
{
WorldManager = worldManager;
}
/// <summary>
/// Gets the colors of the card if possible. Otherwise returns null
/// </summary>
/// <param name="card"></param>
/// <returns></returns>
public d.CardColors GetColors(CardData card)
{
if (card == null) return null;
var gameCard = WorldManager.AllCards.FirstOrDefault(x => x.CardData = card);
if(gameCard == null)
{
return null;
}
else
{
//Force the game card's colors to be set.
SetColorMethodInfo.Invoke(gameCard, new object[] { });
return GetColors(gameCard);
}
}
private d.CardColors GetColors(GameCard gameCard)
{
return new d.CardColors(
ToHtmlColor(((MaterialPropertyBlock)PropBlockFieldInfo.GetValue(gameCard)).GetColor("_Color")),
ToHtmlColor(((MaterialPropertyBlock)PropBlockFieldInfo.GetValue(gameCard)).GetColor("_Color2"))
);
}
/// <summary>
/// Returns a string with RGBA(255,255,255,%) format.
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public string ToHtmlColor(Color color)
{
#pragma warning disable Harmony003 // Harmony non-ref patch parameters modified
return $"rgba({(int)(color.r * 255f)}, {(int)(color.g * 255f)}, {(int)(color.b * 255f)}, {color.a})";
#pragma warning restore Harmony003 // Harmony non-ref patch parameters modified
}
}
}