diff --git a/MiraAPI.Example/ExampleColors.cs b/MiraAPI.Example/ExampleColors.cs
index 297a370e..88710683 100644
--- a/MiraAPI.Example/ExampleColors.cs
+++ b/MiraAPI.Example/ExampleColors.cs
@@ -5,20 +5,14 @@ using UnityEngine;
namespace MiraAPI.Example;
-[RegisterCustomColors]
+[RegisterCustomColors("color.")]
public static class ExampleColors
{
- public static CustomColor Cerulean { get; } = new("color.cerulean", new Color(0.0f, 0.48f, 0.65f))
- {
- ColorBrightness = CustomColorBrightness.Lighter,
- };
+ public static CustomColor Cerulean { get; } = new("cerulean", new Color(0.0f, 0.48f, 0.65f), CustomColorBrightness.Lighter);
- public static CustomColor Rose { get; } = new("color.rose", new Color(0.98f, 0.26f, 0.62f));
+ public static CustomColor Rose { get; } = new("rose", new Color(0.98f, 0.26f, 0.62f), CustomColorBrightness.Lighter);
- public static CustomColor Gold { get; } = new("color.gold", new Color(1.0f, 0.84f, 0.0f));
+ public static CustomColor Gold { get; } = new("gold", new Color(1.0f, 0.84f, 0.0f), CustomColorBrightness.Lighter);
- public static VariableColor Rainbow { get; } = new("color.rainbow", t => new HsbColor(MathUtilities.PingPong(t, 1f, 0.3f), 1f, 1f))
- {
- ColorBrightness = CustomColorBrightness.Lighter,
- };
+ public static VariableColor Rainbow { get; } = new("rainbow", t => new HsbColor(MathUtilities.PingPong(t, 1f, 0.3f), 1f, 1f), CustomColorBrightness.Lighter);
}
diff --git a/MiraAPI/Colors/CustomColor.cs b/MiraAPI/Colors/CustomColor.cs
index f3c38395..d9225e25 100644
--- a/MiraAPI/Colors/CustomColor.cs
+++ b/MiraAPI/Colors/CustomColor.cs
@@ -1,4 +1,5 @@
-using System.Diagnostics.CodeAnalysis;
+using System;
+using System.Diagnostics.CodeAnalysis;
using MiraAPI.Translation;
using MiraAPI.Utilities;
using UnityEngine;
@@ -8,45 +9,44 @@ namespace MiraAPI.Colors;
///
/// Represents a custom color with a main color and a shadow color.
///
-/// The name of the option.
+/// The name of the option as a value.
+/// The name of the option as a value.
/// The main color.
/// The shadow color.
+/// The brightness type of the color.
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "Internal code.")]
-[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:Closing brace should be followed by blank line", Justification = "Looks weird.")]
-public class CustomColor(StringNames name, Color32 mainColor, Color32 shadowColor)
+public class CustomColor(StringNames? nameId, string? rawId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness)
{
- ///
- /// Gets or sets the main color.
- ///
- public Color32 MainColor
- {
- get;
- set
- {
- field = value;
- InternalMainColor = value;
- }
- }
- = mainColor;
+ private StringNames? name = nameId;
+ private CustomColorBrightness colorBrightness = brightness;
///
- /// Gets or sets the shadow color.
+ /// Gets the main color.
///
- public Color32 ShadowColor
- {
- get;
- set
- {
- field = value;
- InternalShadowColor = value;
- }
- }
- = shadowColor;
+ public Color32 MainColor { get; } = mainColor;
///
- /// Gets or sets the name of the color.
+ /// Gets the shadow color.
///
- public StringNames Name { get; set; } = name;
+ public Color32 ShadowColor { get; } = shadowColor;
+
+ ///
+ /// Gets the name of the color.
+ ///
+ public StringNames Name
+ {
+ get
+ {
+ if (!name.HasValue && !string.IsNullOrEmpty(RawId))
+ name = MiraLocaleManager.GetOrCreateLocaleString(RawId);
+
+ return name.GetValueOrDefault();
+ }
+ internal set
+ {
+ name = value;
+ }
+ }
///
/// Gets the id of the color.
@@ -54,28 +54,68 @@ public class CustomColor(StringNames name, Color32 mainColor, Color32 shadowColo
public int ColorId { get; internal set; } = -1;
///
- /// Gets or sets whether the color is lighter or darker. Can be used to help with colorblind stuff.
+ /// Gets or sets whether the color is lighter or darker.
///
- public CustomColorBrightness ColorBrightness { get; set; } = CustomColorBrightness.Darker;
+ public CustomColorBrightness ColorBrightness
+ {
+ get => colorBrightness;
+ [Obsolete("Use the constructor to set ColorBrightness instead. This will be removed in a future version.")]
+ set => colorBrightness = value;
+ }
+ internal string? RawId = rawId;
internal Color InternalMainColor = mainColor;
internal Color InternalShadowColor = shadowColor;
- ///
- public CustomColor(StringNames name, Color32 mainColor)
- : this(name, mainColor, mainColor.DarkenAbsolute(60))
+ ///
+ public CustomColor(StringNames nameId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness)
+ : this(nameId, null, mainColor, shadowColor, brightness)
{
}
- ///
- public CustomColor(string name, Color32 mainColor)
- : this(name, mainColor, mainColor.DarkenAbsolute(60))
+ ///
+ public CustomColor(StringNames nameId, Color32 mainColor, CustomColorBrightness brightness)
+ : this(nameId, null, mainColor, mainColor.DarkenAbsolute(60), brightness)
{
}
- ///
- public CustomColor(string name, Color32 mainColor, Color32 shadowColor)
- : this(MiraLocaleManager.GetOrCreateLocaleString(name), mainColor, shadowColor)
+ ///
+ public CustomColor(string rawId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness)
+ : this(null, rawId, mainColor, shadowColor, brightness)
+ {
+ }
+
+ ///
+ public CustomColor(string rawId, Color32 mainColor, CustomColorBrightness brightness)
+ : this(null, rawId, mainColor, mainColor.DarkenAbsolute(60), brightness)
+ {
+ }
+
+ ///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
+ public CustomColor(StringNames nameId, Color32 mainColor, Color32 shadowColor)
+ : this(nameId, null, mainColor, shadowColor, CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
+ public CustomColor(StringNames nameId, Color32 mainColor)
+ : this(nameId, null, mainColor, mainColor.DarkenAbsolute(60), CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
+ public CustomColor(string rawId, Color32 mainColor, Color32 shadowColor)
+ : this(null, rawId, mainColor, shadowColor, CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ [Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
+ public CustomColor(string rawId, Color32 mainColor)
+ : this(null, rawId, mainColor, mainColor.DarkenAbsolute(60), CustomColorBrightness.Darker)
{
}
}
diff --git a/MiraAPI/Colors/PaletteManager.cs b/MiraAPI/Colors/PaletteManager.cs
index 760822fc..b05c30fd 100644
--- a/MiraAPI/Colors/PaletteManager.cs
+++ b/MiraAPI/Colors/PaletteManager.cs
@@ -50,7 +50,9 @@ public static class PaletteManager
var colors = CustomColors.Select(x => x.MainColor).ToArray();
var shadowColors = CustomColors.Select(x => x.ShadowColor).ToArray();
var stringNames = CustomColors.Select(x => x.Name).ToArray();
+
var newColorId = originalLength;
+
foreach (var color in CustomColors)
{
color.ColorId = newColorId++;
diff --git a/MiraAPI/Colors/RegisterCustomColorsAttribute.cs b/MiraAPI/Colors/RegisterCustomColorsAttribute.cs
index 63565829..b7c96206 100644
--- a/MiraAPI/Colors/RegisterCustomColorsAttribute.cs
+++ b/MiraAPI/Colors/RegisterCustomColorsAttribute.cs
@@ -5,5 +5,20 @@ namespace MiraAPI.Colors;
///
/// Used to mark a class for registration.
///
+/// The group translation ID prefix to attach to color names before registration.
[AttributeUsage(AttributeTargets.Class)]
-public class RegisterCustomColorsAttribute : Attribute;
+public class RegisterCustomColorsAttribute(string idPrefix) : Attribute
+{
+ ///
+ /// Gets the translation ID prefix for all of the colors within the container type.
+ ///
+ public string IdPrefix { get; } = idPrefix;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RegisterCustomColorsAttribute()
+ : this(string.Empty)
+ {
+ }
+}
diff --git a/MiraAPI/Colors/VariableColor.cs b/MiraAPI/Colors/VariableColor.cs
index 84a38358..b3ff8ad1 100644
--- a/MiraAPI/Colors/VariableColor.cs
+++ b/MiraAPI/Colors/VariableColor.cs
@@ -1,6 +1,5 @@
using System;
using MiraAPI.LocalSettings;
-using MiraAPI.Translation;
using MiraAPI.Utilities;
using UnityEngine;
@@ -9,40 +8,129 @@ namespace MiraAPI.Colors;
///
/// Represents a dynamic custom color that changes over time.
///
-/// The name of the color.
+/// The name of the option as a value.
+/// The name of the option as a value.
/// The function to evaluate the dynamic main color.
/// The function to evaluate the dynamic shadow color.
/// The static main color used when dynamic colors are disabled.
/// The static shadow color used when dynamic colors are disabled.
/// The default time that is used to automatically fill in the static colors when the latter is not provided.
-public sealed class VariableColor(StringNames name, Func mainEvaluator, Func? shadowEvaluator, Color32? mainColor = null, Color32? shadowColor = null, float defaultTime = 0f)
- : CustomColor(name, mainColor ?? mainEvaluator(defaultTime), shadowColor ?? shadowEvaluator?.Invoke(defaultTime) ?? (mainColor ?? mainEvaluator(defaultTime)).DarkenAbsolute(60))
+/// The brightness type of the color.
+public sealed class VariableColor(
+ StringNames? nameId,
+ string? rawId,
+ Func mainEvaluator,
+ Func? shadowEvaluator,
+ Color32? mainColor,
+ Color32? shadowColor,
+ float defaultTime,
+ CustomColorBrightness brightness)
+ : CustomColor(
+ nameId,
+ rawId,
+ mainColor ?? mainEvaluator(defaultTime),
+ shadowColor ?? shadowEvaluator?.Invoke(defaultTime) ?? (mainColor ?? mainEvaluator(defaultTime)).DarkenAbsolute(60),
+ brightness)
{
///
- /// Gets or sets a delegate that takes in a time value and returns the dynamic main color.
+ /// Gets a delegate that takes in a time value and returns the dynamic main color.
///
- public Func MainEvaluator { get; set; } = mainEvaluator;
+ public Func MainEvaluator { get; } = mainEvaluator;
///
- /// Gets or sets a delegate that takes in a time value and returns the dynamic shadow color.
+ /// Gets a delegate that takes in a time value and returns the dynamic shadow color.
///
- public Func ShadowEvaluator { get; set; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
+ public Func ShadowEvaluator { get; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
- ///
- public VariableColor(StringNames name, Func mainEvaluator, Color32? mainColor = null, float defaultTime = 0f)
- : this(name, mainEvaluator, null, mainColor, null, defaultTime)
+ ///
+ public VariableColor(
+ StringNames nameId,
+ Func mainEvaluator,
+ Func? shadowEvaluator,
+ CustomColorBrightness brightness,
+ Color32? mainColor = null,
+ Color32? shadowColor = null,
+ float defaultTime = 0f)
+ : this(nameId, null, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, brightness)
{
}
- ///
- public VariableColor(string name, Func mainEvaluator, Color32? mainColor = null, float defaultTime = 0f)
- : this(name, mainEvaluator, null, mainColor, null, defaultTime)
+ ///
+ public VariableColor(
+ StringNames nameId,
+ Func mainEvaluator,
+ CustomColorBrightness brightness,
+ Color32? mainColor = null,
+ float defaultTime = 0f)
+ : this(nameId, null, mainEvaluator, null, mainColor, null, defaultTime, brightness)
{
}
- ///
- public VariableColor(string name, Func mainEvaluator, Func? shadowEvaluator, Color32? mainColor = null, Color32? shadowColor = null, float defaultTime = 0f)
- : this(MiraLocaleManager.GetOrCreateLocaleString(name), mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime)
+ ///
+ public VariableColor(
+ string rawId,
+ Func mainEvaluator,
+ Func? shadowEvaluator,
+ CustomColorBrightness brightness,
+ Color32? mainColor = null,
+ Color32? shadowColor = null,
+ float defaultTime = 0f)
+ : this(null, rawId, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, brightness)
+ {
+ }
+
+ ///
+ public VariableColor(
+ string rawId,
+ Func mainEvaluator,
+ CustomColorBrightness brightness,
+ Color32? mainColor = null,
+ float defaultTime = 0f)
+ : this(null, rawId, mainEvaluator, null, mainColor, null, defaultTime, brightness)
+ {
+ }
+
+ ///
+ public VariableColor(
+ StringNames nameId,
+ Func mainEvaluator,
+ Func? shadowEvaluator,
+ Color32? mainColor = null,
+ Color32? shadowColor = null,
+ float defaultTime = 0f)
+ : this(nameId, null, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ public VariableColor(
+ StringNames nameId,
+ Func mainEvaluator,
+ Color32? mainColor = null,
+ float defaultTime = 0f)
+ : this(nameId, null, mainEvaluator, null, mainColor, null, defaultTime, CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ public VariableColor(
+ string rawId,
+ Func mainEvaluator,
+ Func? shadowEvaluator,
+ Color32? mainColor = null,
+ Color32? shadowColor = null,
+ float defaultTime = 0f)
+ : this(null, rawId, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, CustomColorBrightness.Darker)
+ {
+ }
+
+ ///
+ public VariableColor(
+ string rawId,
+ Func mainEvaluator,
+ Color32? mainColor = null,
+ float defaultTime = 0f)
+ : this(null, rawId, mainEvaluator, null, mainColor, null, defaultTime, CustomColorBrightness.Darker)
{
}
diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs
index 840d8926..e0b7ff38 100644
--- a/MiraAPI/PluginLoading/MiraPluginManager.cs
+++ b/MiraAPI/PluginLoading/MiraPluginManager.cs
@@ -312,7 +312,7 @@ public sealed class MiraPluginManager
{
try
{
- if (type.GetCustomAttribute() == null)
+ if (!type.TryGetCustomAttribute(out var attribute))
{
return false;
}
@@ -336,6 +336,11 @@ public sealed class MiraPluginManager
continue;
}
+ if (!string.IsNullOrEmpty(color.RawId) && !string.IsNullOrEmpty(attribute.IdPrefix))
+ {
+ color.Name = MiraLocaleManager.GetOrCreateLocaleString(attribute.IdPrefix + color.RawId);
+ }
+
PaletteManager.CustomColors.Add(color);
if (color is VariableColor)