Calculate color on the client instead the server

master
D4VID 6 months ago
parent 2d075f6c16
commit 578138fa6c

@ -1,7 +1,7 @@
ID: CriticalPathAnalyzer ID: CriticalPathAnalyzer
Name: CriticalPathAnalyzer Name: CriticalPathAnalyzer
Author: D4VID Author: D4VID
Version: 0.0.4 Version: 0.1.0
Priority: 0 Priority: 0
Dependencies: Dependencies:
- HarmonyForLogicWorld - HarmonyForLogicWorld

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using CriticalPathAnalyzer.Shared.Packets.S2C; using CriticalPathAnalyzer.Shared.Packets.S2C;
using JimmysUnityUtilities; using JimmysUnityUtilities;
@ -25,7 +26,7 @@ namespace CriticalPathAnalyzer.Client.Tool {
private static void HighlightCluster(ClusterDetails cluster) { private static void HighlightCluster(ClusterDetails cluster) {
IWorldData world = Instances.MainWorld.Data; IWorldData world = Instances.MainWorld.Data;
var outline = new OutlineData(new Color24(cluster.Color)); var outline = new OutlineData(new Color24(HsvToRgb(cluster.Time * 20, 1.0f, 1.0f)));
foreach (ComponentAddress address in cluster.ConnectingComponents) { foreach (ComponentAddress address in cluster.ConnectingComponents) {
if (!world.Contains(address)) { if (!world.Contains(address)) {
@ -88,5 +89,56 @@ namespace CriticalPathAnalyzer.Client.Tool {
Outliner.RemoveOutline(address); Outliner.RemoveOutline(address);
} }
} }
/// <summary>
/// Convert HSB to RGB
/// </summary>
/// <param name="h">0 - 360</param>
/// <param name="s">0.0 - 1.0</param>
/// <param name="v">0.0 - 1.0</param>
/// <returns>0xrrggbb</returns>
private static int HsvToRgb(int h, float s, float v) {
s = Math.Clamp(s, 0, 1);
v = Math.Clamp(v, 0, 1);
h = (h % 360 + 360) % 360; // Ensure hue is within 0-359 range
float c = v * s;
float x = c * (1 - Math.Abs((h / 60.0f) % 2 - 1));
float m = v - c;
float r, g, b;
if (h < 60) {
r = c;
g = x;
b = 0;
} else if (h < 120) {
r = x;
g = c;
b = 0;
} else if (h < 180) {
r = 0;
g = c;
b = x;
} else if (h < 240) {
r = 0;
g = x;
b = c;
} else if (h < 300) {
r = x;
g = 0;
b = c;
} else {
r = c;
g = 0;
b = x;
}
int rInt = (byte) ((r + m) * 255);
int gInt = (byte) ((g + m) * 255);
int bInt = (byte) ((b + m) * 255);
return (rInt << 16) | (gInt << 8) | bInt;
}
} }
} }

@ -399,11 +399,15 @@ namespace CriticalPathAnalyzer.Server.Tool {
} }
/// <summary>
/// Obtain additional information about the cluster:
/// Collect all it's pegs and sockets.
/// </summary>
private static ClusterDetails CollectClusterInformation(Cluster cluster, int time) { private static ClusterDetails CollectClusterInformation(Cluster cluster, int time) {
var details = new ClusterDetails { var details = new ClusterDetails {
Pegs = new List<PegAddress>(), Pegs = new List<PegAddress>(),
ConnectingComponents = new List<ComponentAddress>(), ConnectingComponents = new List<ComponentAddress>(),
Color = HsvToRgb(time * 20, 1, 1), Time = time,
}; };
// Two lists are never null, according to how it is created and used: // Two lists are never null, according to how it is created and used:
@ -418,59 +422,9 @@ namespace CriticalPathAnalyzer.Server.Tool {
} }
} }
foreach (OutputPeg peg in outputPegs) { details.Pegs.AddRange(outputPegs.Select(peg => peg.Address));
details.Pegs.Add(peg.Address);
}
return details; return details;
} }
/// <param name="h">0 - 360</param>
/// <param name="s">0.0 - 1.0</param>
/// <param name="v">0.0 - 1.0</param>
/// <returns>0xrrggbb</returns>
private static int HsvToRgb(int h, float s, float v) {
s = Math.Clamp(s, 0, 1);
v = Math.Clamp(v, 0, 1);
h = (h % 360 + 360) % 360; // Ensure hue is within 0-359 range
float c = v * s;
float x = c * (1 - Math.Abs((h / 60.0f) % 2 - 1));
float m = v - c;
float r, g, b;
if (h < 60) {
r = c;
g = x;
b = 0;
} else if (h < 120) {
r = x;
g = c;
b = 0;
} else if (h < 180) {
r = 0;
g = c;
b = x;
} else if (h < 240) {
r = 0;
g = x;
b = c;
} else if (h < 300) {
r = x;
g = 0;
b = c;
} else {
r = c;
g = 0;
b = x;
}
int rInt = (byte) ((r + m) * 255);
int gInt = (byte) ((g + m) * 255);
int bInt = (byte) ((b + m) * 255);
return (rInt << 16) | (gInt << 8) | bInt;
}
} }
} }

@ -27,6 +27,9 @@ namespace CriticalPathAnalyzer.Shared.Packets.S2C {
/// </summary> /// </summary>
[Key(1)] public List<ComponentAddress> ConnectingComponents; [Key(1)] public List<ComponentAddress> ConnectingComponents;
[Key(3)] public int Color; /// <summary>
/// Delay in ticks after which this cluster gets last updated
/// </summary>
[Key(2)] public int Time;
} }
} }
Loading…
Cancel
Save