Skip to content

Commit 226b341

Browse files
committed
Refactor placeholder color handling and add ColorExtensions utility methods
1 parent a38c71a commit 226b341

File tree

6 files changed

+84
-11
lines changed

6 files changed

+84
-11
lines changed

Assets/_PackageRoot/Runtime/Future/Extensions/FutureEx.Placeholder.Color.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IFuture<Texture2D> SetPlaceholder(this IFuture<Texture2D> future,
2323
/// <param name="color">hex color to fill solid color</param>
2424
/// <returns>Returns the Future instance</returns>
2525
public static IFuture<Texture2D> SetPlaceholder(this IFuture<Texture2D> future, Color color, params PlaceholderTrigger[] triggers)
26-
=> future.SetPlaceholder(ColorUtility.ToHtmlStringRGBA(color), triggers);
26+
=> future.SetPlaceholder(color.ToHexRGBA(), triggers);
2727

2828
/// <summary>
2929
/// Set a placeholder in all conditions for this Future instance
@@ -42,7 +42,7 @@ public static IFuture<Sprite> SetPlaceholder(this IFuture<Sprite> future, Color
4242
/// <param name="hexColor">hex color to fill solid color</param>
4343
/// <returns>Returns the Future instance</returns>
4444
public static IFuture<Sprite> SetPlaceholder(this IFuture<Sprite> future, Color color, params PlaceholderTrigger[] triggers)
45-
=> future.SetPlaceholder(ColorUtility.ToHtmlStringRGBA(color), triggers);
45+
=> future.SetPlaceholder(color.ToHexRGBA(), triggers);
4646

4747
/// <summary>
4848
/// Set a placeholder in all conditions for this Future instance
@@ -61,7 +61,7 @@ public static IFuture<Reference<Texture2D>> SetPlaceholder(this IFuture<Referenc
6161
/// <param name="hexColor">hex color to fill solid color</param>
6262
/// <returns>Returns the Future instance</returns>
6363
public static IFuture<Reference<Texture2D>> SetPlaceholder(this IFuture<Reference<Texture2D>> future, Color color, params PlaceholderTrigger[] triggers)
64-
=> future.SetPlaceholder(ColorUtility.ToHtmlStringRGBA(color), triggers);
64+
=> future.SetPlaceholder(color.ToHexRGBA(), triggers);
6565

6666
/// <summary>
6767
/// Set a placeholder in all conditions for this Future instance
@@ -80,6 +80,6 @@ public static IFuture<Reference<Sprite>> SetPlaceholder(this IFuture<Reference<S
8080
/// <param name="hexColor">hex color to fill solid color</param>
8181
/// <returns>Returns the Future instance</returns>
8282
public static IFuture<Reference<Sprite>> SetPlaceholder(this IFuture<Reference<Sprite>> future, Color color, params PlaceholderTrigger[] triggers)
83-
=> future.SetPlaceholder(ColorUtility.ToHtmlStringRGBA(color), triggers);
83+
=> future.SetPlaceholder(color.ToHexRGBA(), triggers);
8484
}
8585
}

Assets/_PackageRoot/Runtime/Future/Future.Placeholder.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public IFuture<T> SetPlaceholder(T placeholder, params PlaceholderTrigger[] trig
1919
return this;
2020
}
2121

22+
if (triggers == null || triggers.Length == 0)
23+
{
24+
if (LogLevel.IsActive(DebugLevel.Error))
25+
Debug.LogError($"[ImageLoader] Future[id={Id}] SetPlaceholder: triggers are not specified\n{Url}");
26+
return this;
27+
}
28+
29+
if (LogLevel.IsActive(DebugLevel.Trace))
30+
Debug.Log($"[ImageLoader] Future[id={Id}] SetPlaceholder\n{Url}");
31+
2232
foreach (var trigger in triggers)
2333
{
2434
if (trigger.IsEqual(Status))
@@ -28,7 +38,7 @@ public IFuture<T> SetPlaceholder(T placeholder, params PlaceholderTrigger[] trig
2838
foreach (var setter in consumers)
2939
Safe.Run(setter, placeholder, LogLevel);
3040
}
31-
continue;
41+
// continue;
3242
}
3343
lock (placeholders)
3444
placeholders[trigger.AsFutureStatus()] = placeholder;

Assets/_PackageRoot/Runtime/Future/Future.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ void FeedConsumers(T value)
225225
lock (consumers)
226226
{
227227
foreach (var setter in consumers)
228+
{
229+
if (LogLevel.IsActive(DebugLevel.Trace))
230+
Debug.Log($"[ImageLoader] Future[id={Id}] Feed consumer\n{Url}");
228231
Safe.Run(setter, value, LogLevel);
232+
}
229233
}
230234
}
231235

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using UnityEngine;
2+
3+
namespace Extensions.Unity.ImageLoader
4+
{
5+
public static class ColorExtensions
6+
{
7+
public static string ToHexRGB(this Color color, bool hexSymbol = true) => hexSymbol
8+
? $"#{ColorUtility.ToHtmlStringRGB(color)}"
9+
: ColorUtility.ToHtmlStringRGB(color);
10+
11+
public static string ToHexRGBA(this Color color, bool hexSymbol = true) => hexSymbol
12+
? $"#{ColorUtility.ToHtmlStringRGBA(color)}"
13+
: ColorUtility.ToHtmlStringRGBA(color);
14+
15+
public static Color SetR(this Color color, float r)
16+
{
17+
color.r = r;
18+
return color;
19+
}
20+
public static Color SetG(this Color color, float g)
21+
{
22+
color.g = g;
23+
return color;
24+
}
25+
public static Color SetB(this Color color, float b)
26+
{
27+
color.b = b;
28+
return color;
29+
}
30+
public static Color SetA(this Color color, float a)
31+
{
32+
color.a = a;
33+
return color;
34+
}
35+
public static bool HexToColor(this string hex, out Color color)
36+
=> ColorUtility.TryParseHtmlString(hex, out color);
37+
38+
public static Color HexToColor(this string hex)
39+
{
40+
if (!ColorUtility.TryParseHtmlString(hex, out var color))
41+
{
42+
if (ImageLoader.settings.debugLevel.IsActive(DebugLevel.Error))
43+
Debug.LogError($"[ImageLoader] Invalid hex color: {hex}");
44+
}
45+
return color;
46+
}
47+
}
48+
}

Assets/_PackageRoot/Runtime/Utils/ColorExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_PackageRoot/Samples/SamplePlaceholder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ public class SamplePlaceholder : MonoBehaviour
66
{
77
[SerializeField] string imageURL; // URL of the image to be loaded
88
[SerializeField] Image image; // UI Image component to display the loaded sprite
9-
[SerializeField] Sprite placeholder1;
10-
[SerializeField] Sprite placeholder2;
11-
[SerializeField] Sprite placeholder3;
9+
[SerializeField] Sprite placeholderAny;
10+
[SerializeField] Sprite placeholderLoadingFromSource;
11+
[SerializeField] Color placeholderFailedToLoad = Color.red;
1212

1313
void Start()
1414
{
1515
ImageLoader.LoadSprite(imageURL)
1616
// set placeholder in all conditions
17-
.SetPlaceholder(placeholder1)
17+
.SetPlaceholder(placeholderAny)
1818

1919
// set placeholder in a specific conditions
20-
.SetPlaceholder(placeholder2, PlaceholderTrigger.LoadingFromSource)
21-
.SetPlaceholder(placeholder3, PlaceholderTrigger.FailedToLoad)
20+
.SetPlaceholder(placeholderLoadingFromSource, PlaceholderTrigger.LoadingFromSource)
21+
.SetPlaceholder(placeholderFailedToLoad, PlaceholderTrigger.FailedToLoad)
2222

2323
// set consumer
2424
.Consume(image)

0 commit comments

Comments
 (0)