From e6194573d58b18ca6214da14fa964a917006dd1d Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Thu, 7 May 2020 20:35:11 -0700 Subject: [PATCH] Color stuff. Fixed ToString for util structs --- src/Game.bf | 2 +- src/Util/Color.bf | 59 +++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/Game.bf b/src/Game.bf index 59e3c5f..5f60097 100644 --- a/src/Game.bf +++ b/src/Game.bf @@ -26,7 +26,7 @@ namespace Strawberry private bool updating; public SDL.Renderer* Renderer { get; private set; } - public Color ClearColor = Color.Black; + public Color ClearColor = .Black; private SDL.Rect screenRect; private SDL.Window* window; diff --git a/src/Util/Color.bf b/src/Util/Color.bf index 189d63d..dd47059 100644 --- a/src/Util/Color.bf +++ b/src/Util/Color.bf @@ -4,19 +4,22 @@ namespace Strawberry { public struct Color { - static public readonly Color White = Color(255, 255, 255); - static public readonly Color Black = Color(0, 0, 0); - static public readonly Color Transparent = Color(0, 0, 0, 0); - static public readonly Color Red = Color(255, 0, 0); - static public readonly Color Green = Color(0, 255, 0); - static public readonly Color Blue = Color(0, 0, 255); + static public readonly Color White = 0xFFFFFFFF; + static public readonly Color Black = 0x000000FF; + static public readonly Color Transparent = 0x00000000; + static public readonly Color Red = 0xFF0000FF; + static public readonly Color Green = 0x00FF00FF; + static public readonly Color Blue = 0x0000FFFF; + static public readonly Color Cyan = 0xFF00FFFF; + static public readonly Color Magenta = 0xFFFF00FF; + static public readonly Color Yellow = 0x00FFFFFF; public uint8 R; public uint8 G; public uint8 B; public uint8 A; - public this(uint8 red, uint8 green, uint8 blue, uint8 alpha) + public this(uint8 red, uint8 green, uint8 blue, uint8 alpha = 255) { R = red; G = green; @@ -24,13 +27,7 @@ namespace Strawberry A = alpha; } - public this(uint8 red, uint8 green, uint8 blue) - : this(red, green, blue, 255) - { - - } - - public this(float red, float green, float blue, float alpha) + public this(float red, float green, float blue, float alpha = 1f) { R = (uint8)(red * 255); G = (uint8)(green * 255); @@ -38,12 +35,6 @@ namespace Strawberry A = (uint8)(alpha * 255); } - public this(float red, float green, float blue) - : this(red, green, blue, 1f) - { - - } - public float Rf { [Inline] @@ -104,6 +95,19 @@ namespace Strawberry } } + public override void ToString(String strBuffer) + { + strBuffer.Set("Color [ "); + ((uint)R).ToString(strBuffer); + strBuffer.Append(", "); + ((uint)G).ToString(strBuffer); + strBuffer.Append(", "); + ((uint)B).ToString(strBuffer); + strBuffer.Append(", "); + ((uint)A).ToString(strBuffer); + strBuffer.Append(" ]"); + } + static public Color Lerp(Color a, Color b, float t) { return Color( @@ -114,6 +118,21 @@ namespace Strawberry ); } + static public implicit operator Color(uint32 from) + { + return Color( + (uint8)((from >> 24) & 0xFF), + (uint8)((from >> 16) & 0xFF), + (uint8)((from >> 8) & 0xFF), + (uint8)(from & 0xFF) + ); + } + + static public implicit operator uint32(Color from) + { + return (((uint32)from.R) << 24) | (((uint32)from.G) << 16) | (((uint32)from.B) << 8) | ((uint32)from.A); + } + static public Color operator/(Color a, Color b) { return Lerp(a, b, 0.5f);