Refactor Color.FromRGBA and add matching old logic Color.FromARGB

2592
This commit is contained in:
Wojtek Figat
2024-05-16 13:45:29 +02:00
parent 3404643636
commit 2529312152
9 changed files with 133 additions and 51 deletions

View File

@@ -74,6 +74,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CppNaming/UserRules/=UNION/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CppNaming/UserRules/=UNION_005FMEMBER/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AI/@EntryIndexedValue">AI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARGB/@EntryIndexedValue">ARGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LO/@EntryIndexedValue">LO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RPC/@EntryIndexedValue">RPC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SDK/@EntryIndexedValue">SDK</s:String>

View File

@@ -39,7 +39,7 @@ namespace FlaxEditor.CustomEditors.Editors
if (watermarkAttribute is WatermarkAttribute watermark)
{
_watermarkText = watermark.WatermarkText;
var watermarkColor = watermark.WatermarkColor > 0 ? Color.FromRGBA(watermark.WatermarkColor) : FlaxEngine.GUI.Style.Current.ForegroundDisabled;
var watermarkColor = watermark.WatermarkColor > 0 ? Color.FromRGB(watermark.WatermarkColor) : FlaxEngine.GUI.Style.Current.ForegroundDisabled;
_watermarkColor = watermarkColor;
_element.TextBox.WatermarkText = watermark.WatermarkText;
_element.TextBox.WatermarkTextColor = watermarkColor;

View File

@@ -318,7 +318,7 @@ namespace FlaxEditor.CustomEditors
if (header.FontSize > 0)
element.Label.Font = new FontReference(element.Label.Font.Font, header.FontSize);
if (header.Color > 0)
element.Label.TextColor = Color.FromRGBA(header.Color);
element.Label.TextColor = Color.FromRGB(header.Color);
return element;
}

View File

@@ -32,41 +32,39 @@ Color::Color(const Color32& color)
{
}
Color Color::FromHex(const String& hexString, bool& isValid)
Color Color::FromHex(const String& hex, bool& isValid)
{
int32 r, g, b, a = 255;
isValid = true;
int32 startIndex = !hexString.IsEmpty() && hexString[0] == Char('#') ? 1 : 0;
if (hexString.Length() == 3 + startIndex)
int32 startIndex = !hex.IsEmpty() && hex[0] == Char('#') ? 1 : 0;
if (hex.Length() == 3 + startIndex)
{
r = StringUtils::HexDigit(hexString[startIndex++]);
g = StringUtils::HexDigit(hexString[startIndex++]);
b = StringUtils::HexDigit(hexString[startIndex]);
r = StringUtils::HexDigit(hex[startIndex++]);
g = StringUtils::HexDigit(hex[startIndex++]);
b = StringUtils::HexDigit(hex[startIndex]);
r = (r << 4) + r;
g = (g << 4) + g;
b = (b << 4) + b;
}
else if (hexString.Length() == 6 + startIndex)
else if (hex.Length() == 6 + startIndex)
{
r = (StringUtils::HexDigit(hexString[startIndex + 0]) << 4) + StringUtils::HexDigit(hexString[startIndex + 1]);
g = (StringUtils::HexDigit(hexString[startIndex + 2]) << 4) + StringUtils::HexDigit(hexString[startIndex + 3]);
b = (StringUtils::HexDigit(hexString[startIndex + 4]) << 4) + StringUtils::HexDigit(hexString[startIndex + 5]);
r = (StringUtils::HexDigit(hex[startIndex + 0]) << 4) + StringUtils::HexDigit(hex[startIndex + 1]);
g = (StringUtils::HexDigit(hex[startIndex + 2]) << 4) + StringUtils::HexDigit(hex[startIndex + 3]);
b = (StringUtils::HexDigit(hex[startIndex + 4]) << 4) + StringUtils::HexDigit(hex[startIndex + 5]);
}
else if (hexString.Length() == 8 + startIndex)
else if (hex.Length() == 8 + startIndex)
{
r = (StringUtils::HexDigit(hexString[startIndex + 0]) << 4) + StringUtils::HexDigit(hexString[startIndex + 1]);
g = (StringUtils::HexDigit(hexString[startIndex + 2]) << 4) + StringUtils::HexDigit(hexString[startIndex + 3]);
b = (StringUtils::HexDigit(hexString[startIndex + 4]) << 4) + StringUtils::HexDigit(hexString[startIndex + 5]);
a = (StringUtils::HexDigit(hexString[startIndex + 6]) << 4) + StringUtils::HexDigit(hexString[startIndex + 7]);
r = (StringUtils::HexDigit(hex[startIndex + 0]) << 4) + StringUtils::HexDigit(hex[startIndex + 1]);
g = (StringUtils::HexDigit(hex[startIndex + 2]) << 4) + StringUtils::HexDigit(hex[startIndex + 3]);
b = (StringUtils::HexDigit(hex[startIndex + 4]) << 4) + StringUtils::HexDigit(hex[startIndex + 5]);
a = (StringUtils::HexDigit(hex[startIndex + 6]) << 4) + StringUtils::HexDigit(hex[startIndex + 7]);
}
else
{
r = g = b = 0;
isValid = false;
}
return FromBytes(r, g, b, a);
}
@@ -122,8 +120,9 @@ String Color::ToHexString() const
const byte r = static_cast<byte>(R * MAX_uint8);
const byte g = static_cast<byte>(G * MAX_uint8);
const byte b = static_cast<byte>(B * MAX_uint8);
const byte a = static_cast<byte>(A * MAX_uint8);
Char result[6];
Char result[8];
result[0] = digits[r >> 4 & 0x0f];
result[1] = digits[r & 0x0f];
@@ -134,7 +133,10 @@ String Color::ToHexString() const
result[4] = digits[b >> 4 & 0x0f];
result[5] = digits[b & 0x0f];
return String(result, 6);
result[6] = digits[a >> 4 & 0x0f];
result[7] = digits[a & 0x0f];
return String(result, 8);
}
bool Color::IsTransparent() const

View File

@@ -230,36 +230,93 @@ namespace FlaxEngine
}
/// <summary>
/// Creates <see cref="Color"/> from the RGB value and separate alpha channel.
/// Creates <see cref="Color"/> from the RGB value (bottom bits contain Blue) and separate alpha channel.
/// </summary>
/// <param name="rgb">The packed RGB value.</param>
/// <param name="rgb">The packed RGB value (bottom bits contain Blue).</param>
/// <param name="a">The alpha channel value.</param>
/// <returns>The color.</returns>
public static Color FromRGB(uint rgb, float a = 1.0f)
{
return new Color(
((rgb >> 16) & 0xff) / 255.0f,
((rgb >> 8) & 0xff) / 255.0f,
(rgb & 0xff) / 255.0f,
a);
return new Color(((rgb >> 16) & 0xff) / 255.0f, ((rgb >> 8) & 0xff) / 255.0f, (rgb & 0xff) / 255.0f, a);
}
/// <summary>
/// Creates <see cref="Color"/> from the RGBA value.
/// Creates <see cref="Color"/> from the ARGB value (bottom bits contain Blue).
/// </summary>
/// <param name="rgb">The packed RGBA value.</param>
/// <param name="argb">The packed ARGB value (bottom bits contain Blue).</param>
/// <returns>The color.</returns>
public static Color FromRGBA(uint rgb)
public static Color FromARGB(uint argb)
{
return new Color(
((rgb >> 16) & 0xff) / 255.0f,
((rgb >> 8) & 0xff) / 255.0f,
(rgb & 0xff) / 255.0f,
((rgb >> 24) & 0xff) / 255.0f);
return new Color(((argb >> 16) & 0xff) / 255.0f, ((argb >> 8) & 0xff) / 255.0f, ((argb >> 0) & 0xff) / 255.0f, ((argb >> 24) & 0xff) / 255.0f);
}
/// <summary>
/// Gets the color value as the hexadecimal string.
/// Creates <see cref="Color"/> from the RGBA value (bottom bits contain Alpha).
/// </summary>
/// <param name="rgba">The packed RGBA value (bottom bits Alpha Red).</param>
/// <returns>The color.</returns>
public static Color FromRGBA(uint rgba)
{
return new Color(((rgba >> 24) & 0xff) / 255.0f, ((rgba >> 16) & 0xff) / 255.0f, ((rgba >> 8) & 0xff) / 255.0f, ((rgba >> 0) & 0xff) / 255.0f);
}
/// <summary>
/// Creates <see cref="Color"/> from the Hex string.
/// </summary>
/// <param name="hex">The hexadecimal color string.</param>
/// <returns>The output color value.</returns>
public static Color FromHex(string hex)
{
FromHex(hex, out var color);
return color;
}
/// <summary>
/// Creates <see cref="Color"/> from the Hex string.
/// </summary>
/// <param name="hex">The hexadecimal color string.</param>
/// <param name="color">The output color value. Valid if method returns true.</param>
/// <returns>True if method was able to convert color, otherwise false.</returns>
public static bool FromHex(string hex, out Color color)
{
int r, g, b, a = 255;
bool isValid = true;
int startIndex = hex.Length != 0 && hex[0] == '#' ? 1 : 0;
if (hex.Length == 3 + startIndex)
{
r = StringUtils.HexDigit(hex[startIndex++]);
g = StringUtils.HexDigit(hex[startIndex++]);
b = StringUtils.HexDigit(hex[startIndex]);
r = (r << 4) + r;
g = (g << 4) + g;
b = (b << 4) + b;
}
else if (hex.Length == 6 + startIndex)
{
r = (StringUtils.HexDigit(hex[startIndex + 0]) << 4) + StringUtils.HexDigit(hex[startIndex + 1]);
g = (StringUtils.HexDigit(hex[startIndex + 2]) << 4) + StringUtils.HexDigit(hex[startIndex + 3]);
b = (StringUtils.HexDigit(hex[startIndex + 4]) << 4) + StringUtils.HexDigit(hex[startIndex + 5]);
}
else if (hex.Length == 8 + startIndex)
{
r = (StringUtils.HexDigit(hex[startIndex + 0]) << 4) + StringUtils.HexDigit(hex[startIndex + 1]);
g = (StringUtils.HexDigit(hex[startIndex + 2]) << 4) + StringUtils.HexDigit(hex[startIndex + 3]);
b = (StringUtils.HexDigit(hex[startIndex + 4]) << 4) + StringUtils.HexDigit(hex[startIndex + 5]);
a = (StringUtils.HexDigit(hex[startIndex + 6]) << 4) + StringUtils.HexDigit(hex[startIndex + 7]);
}
else
{
r = g = b = 0;
isValid = false;
}
color = new Color(r, g, b, a);
return isValid;
}
/// <summary>
/// Gets the color value as the hexadecimal string (in RGBA order).
/// </summary>
/// <returns>Hex string.</returns>
public string ToHexString()
@@ -287,7 +344,7 @@ namespace FlaxEngine
return new string(result);
}
/// <summary>
/// Creates <see cref="Color"/> from the text string (hex or color name).
/// </summary>

View File

@@ -126,9 +126,9 @@ public:
}
/// <summary>
/// Initializes from packed RGB value of the color and separate alpha channel value.
/// Initializes from packed RGB value (bottom bits contain Blue) of the color and separate alpha channel value.
/// </summary>
/// <param name="rgb">The packed RGB value.</param>
/// <param name="rgb">The packed RGB value (bottom bits contain Blue).</param>
/// <param name="a">The alpha channel.</param>
/// <returns>The color.</returns>
static Color FromRGB(uint32 rgb, float a = 1.0f)
@@ -137,22 +137,32 @@ public:
}
/// <summary>
/// Initializes from packed RGBA value.
/// Initializes from packed ARGB value (bottom bits contain Blue).
/// </summary>
/// <param name="rgba">The packed RGBA value.</param>
/// <param name="argb">The packed ARGB value (bottom bits contain Blue).</param>
/// <returns>The color.</returns>
static Color FromARGB(uint32 argb)
{
return Color((float)((argb >> 16) & 0xff) / 255.0f,(float)((argb >> 8) & 0xff) / 255.0f, (float)((argb >> 0) & 0xff) / 255.0f, (float)((argb >> 24) & 0xff) / 255.0f);
}
/// <summary>
/// Initializes from packed RGBA value (bottom bits contain Alpha).
/// </summary>
/// <param name="rgba">The packed RGBA value (bottom bits contain Alpha).</param>
/// <returns>The color.</returns>
static Color FromRGBA(uint32 rgba)
{
return Color(static_cast<float>(rgba >> 16 & 0xff) / 255.0f, static_cast<float>(rgba >> 8 & 0xff) / 255.0f, static_cast<float>(rgba & 0xff) / 255.0f, static_cast<float>(rgba >> 24 & 0xff) / 255.0f);
return Color((float)((rgba >> 24) & 0xff) / 255.0f,(float)((rgba >> 16) & 0xff) / 255.0f, (float)((rgba >> 8) & 0xff) / 255.0f, (float)((rgba >> 0) & 0xff) / 255.0f);
}
static Color FromHex(const String& hexString)
static Color FromHex(const String& hex)
{
bool isValid;
return FromHex(hexString, isValid);
return FromHex(hex, isValid);
}
static Color FromHex(const String& hexString, bool& isValid);
static Color FromHex(const String& hex, bool& isValid);
/// <summary>
/// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1].

View File

@@ -23,11 +23,14 @@ namespace FlaxEngine
public int FontSize;
/// <summary>
/// The custom header color (as 32-bit uint).
/// The custom header color (as 32-bit uint in RGB order, bottom bits contain Blue).
/// </summary>
public uint Color;
private HeaderAttribute()
/// <summary>
/// Initializes a new instance of the <see cref="HeaderAttribute"/> class.
/// </summary>
public HeaderAttribute()
{
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
namespace FlaxEngine;
@@ -15,7 +15,7 @@ public class WatermarkAttribute : Attribute
public string WatermarkText;
/// <summary>
/// The watermark color.
/// The watermark color (as 32-bit uint in RGB order, bottom bits contain Blue).
/// </summary>
public uint WatermarkColor;
@@ -26,7 +26,7 @@ public class WatermarkAttribute : Attribute
public WatermarkAttribute(string text)
{
WatermarkText = text;
WatermarkColor = 0; // default color of watermark in textbox
WatermarkColor = 0; // Default color of watermark in textbox
}
/// <summary>

View File

@@ -37,6 +37,15 @@ namespace FlaxEngine.Tests
Assert.AreEqual(Color.Maroon, ColorHSV.FromColor(Color.Maroon).ToColor());
Assert.AreEqual(new Color(184, 209, 219, 255).ToRgba(), ColorHSV.FromColor(new Color(184, 209, 219, 255)).ToColor().ToRgba());
}
[Test]
public void TestHexConversion()
{
String hex = Color.Blue.AlphaMultiplied(0.5f).ToHexString();
Color col1 = Color.FromHex(hex);
Color col2 = Color.FromRGBA(0x0000FF7F);
Assert.AreEqual((Color32)col1, (Color32)col2);
}
}
}
#endif