Skip to content

Commit

Permalink
feat: Higher bit color types
Browse files Browse the repository at this point in the history
  • Loading branch information
stifskere committed Sep 25, 2023
1 parent d986dcf commit bd1dae3
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 8 deletions.
82 changes: 82 additions & 0 deletions Colors/Rgb48.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,88 @@
using JetBrains.Annotations;

namespace MemwLib.Colors;

public class Rgb48
{
[PublicAPI]
public ushort R { get; set; }

[PublicAPI]
public ushort G { get; set; }

[PublicAPI]
public ushort B { get; set; }

public Rgb48(ushort r, ushort g, ushort b)
{
R = r;
G = g;
B = b;
}

public Rgb48(ulong color)
: this((ushort)((color >> 32) & 0xFFFF), (ushort)((color >> 16) & 0xFFFF), (ushort)(color & 0xFFFF))
{
if (color > 0xFFFFFFFFFFFF)
throw new ArgumentException("Color value exceeds maximum unsigned short value.", nameof(color));
}

[PublicAPI]
public virtual ulong ToUlong()
=> (ulong)R << 32 | (ulong)G << 16 | B;

public static explicit operator ulong(Rgb48 instance)
=> instance.ToUlong();

public static explicit operator Rgb48(ulong instance)
=> new(instance);

[PublicAPI] public static readonly Rgb48 Pink = (Rgb48)0xffffc0cb;
[PublicAPI] public static readonly Rgb48 Crimson = (Rgb48)0xffdc143c;
[PublicAPI] public static readonly Rgb48 Red = (Rgb48)0xffff0000;
[PublicAPI] public static readonly Rgb48 Maroon = (Rgb48)0xff800000;
[PublicAPI] public static readonly Rgb48 Brown = (Rgb48)0xffa52a2a;
[PublicAPI] public static readonly Rgb48 MistyRose = (Rgb48)0xffffe4e1;
[PublicAPI] public static readonly Rgb48 Salmon = (Rgb48)0xfffa8072;
[PublicAPI] public static readonly Rgb48 Coral = (Rgb48)0xffff7f50;
[PublicAPI] public static readonly Rgb48 OrangeRed = (Rgb48)0xffff4500;
[PublicAPI] public static readonly Rgb48 Chocolate = (Rgb48)0xffd2691e;
[PublicAPI] public static readonly Rgb48 Orange = (Rgb48)0xffe1d79d;
[PublicAPI] public static readonly Rgb48 Gold = (Rgb48)0xffffd700;
[PublicAPI] public static readonly Rgb48 Ivory = (Rgb48)0xfffffff0;
[PublicAPI] public static readonly Rgb48 Yellow = (Rgb48)0xffffff00;
[PublicAPI] public static readonly Rgb48 Olive = (Rgb48)0xff808000;
[PublicAPI] public static readonly Rgb48 YellowGreen = (Rgb48)0xff9acd32;
[PublicAPI] public static readonly Rgb48 LawnGreen = (Rgb48)0xff7cfc00;
[PublicAPI] public static readonly Rgb48 Chartreuse = (Rgb48)0xff7fff00;
[PublicAPI] public static readonly Rgb48 Lime = (Rgb48)0xff00ff00;
[PublicAPI] public static readonly Rgb48 Green = (Rgb48)0xff008000;
[PublicAPI] public static readonly Rgb48 SpringGreen = (Rgb48)0xff00ff7f;
[PublicAPI] public static readonly Rgb48 Aquamarine = (Rgb48)0xff7fffd4;
[PublicAPI] public static readonly Rgb48 Turquoise = (Rgb48)0xff40e0d0;
[PublicAPI] public static readonly Rgb48 Azure = (Rgb48)0xfff0ffff;
[PublicAPI] public static readonly Rgb48 AquaCyan = (Rgb48)0xffc2c2c2;
[PublicAPI] public static readonly Rgb48 Teal = (Rgb48)0xff008080;
[PublicAPI] public static readonly Rgb48 Lavender = (Rgb48)0xffe6e6fa;
[PublicAPI] public static readonly Rgb48 Blue = (Rgb48)0xff0000ff;
[PublicAPI] public static readonly Rgb48 Navy = (Rgb48)0xff000080;
[PublicAPI] public static readonly Rgb48 BlueViolet = (Rgb48)0xff8a2be2;
[PublicAPI] public static readonly Rgb48 Indigo = (Rgb48)0xff4b0082;
[PublicAPI] public static readonly Rgb48 DarkViolet = (Rgb48)0xff9400d3;
[PublicAPI] public static readonly Rgb48 Plum = (Rgb48)0xffdda0dd;
[PublicAPI] public static readonly Rgb48 Magenta = (Rgb48)0xffff00ff;
[PublicAPI] public static readonly Rgb48 Purple = (Rgb48)0xffbf9dbf;
[PublicAPI] public static readonly Rgb48 RedViolet = (Rgb48)0xffc71585;
[PublicAPI] public static readonly Rgb48 Tan = (Rgb48)0xffd2b48c;
[PublicAPI] public static readonly Rgb48 Beige = (Rgb48)0xfff5f5dc;
[PublicAPI] public static readonly Rgb48 SlateGray = (Rgb48)0xff708090;
[PublicAPI] public static readonly Rgb48 DarkSlateGray = (Rgb48)0xff2f4f4f;
[PublicAPI] public static readonly Rgb48 White = (Rgb48)0xffffffff;
[PublicAPI] public static readonly Rgb48 SmokeWhite = (Rgb48)0xfff5f5f5;
[PublicAPI] public static readonly Rgb48 LightGray = (Rgb48)0xffd3d3d3;
[PublicAPI] public static readonly Rgb48 Silver = (Rgb48)0xffc0c0c0;
[PublicAPI] public static readonly Rgb48 DarkGray = (Rgb48)0xffa9a9a9;
[PublicAPI] public static readonly Rgb48 Gray = (Rgb48)0xff808080;
[PublicAPI] public static readonly Rgb48 DimGray = (Rgb48)0xff696969;
[PublicAPI] public static readonly Rgb48 Black = (Rgb48)0xff000000;
}
28 changes: 26 additions & 2 deletions Colors/Rgb64.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
using JetBrains.Annotations;

namespace MemwLib.Colors;

public class Rgb64
public class Rgb64 : Rgb48
{

[PublicAPI]
public ushort A { get; set; }

public Rgb64(ushort r, ushort g, ushort b, ushort a) : base(r, g, b)
{
A = a;
}

public Rgb64(ulong color) : base(color >> 16)
{
A = (ushort)(color & 0xFFFF);
}

public Rgb64(Rgb48 color) : this(((ulong)65535 << 48) | (ulong)color) { }

public override ulong ToUlong()
=> (ulong)R << 48 | (ulong)G << 32 | (ulong)B << 16 | A;

public static explicit operator ulong(Rgb64 instance)
=> instance.ToUlong();

public static explicit operator Rgb64(ulong instance)
=> new(instance);
}
44 changes: 38 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ HttpServer server = new(IpAdress.Any, 3000);
// (IpAdress ip, ushort port)
// (IpAdress ip, ushort port, CancellationToken cancellationToken)
// Method type supports flags and path supports Regex c# api.
server.AddEndpoint(RequestMethodType.Get | RequestMethodType.Put, "/test", request => {
return new ResponseEntity(200, "Hello World!");
// (int code, string httpVersion, string body)
// (int code, string body)
});

// thread blocking due to implementation.
// (RequestMethodType method, string path, EndpointHandlerDelegate handler)
// (RequestMethodType method, Regex path, EndpointHandlerDelegate handler)
```

Notice how using an HttpServer instance will block the running thread, it is advised to use a CancellationToken to unblock the thread and stop the server.

### Colors

Rgb color implementation in 2 classes `<Rgb24>` and `<Rgb32>`, for future usage in other library parts.
Rgb color implementation in 4 classes `<Rgb24>`, `<Rgb32>`, `<Rgb48>` and `<Rgb64>`, for future usage in other library parts.

Basic usage demonstration:
```csharp
Expand All @@ -48,10 +49,41 @@ Rgb32 red = (Rgb32)0xFF0000FF;
Rgb32 green = new Rgb32(0x00FF00FF);
Rgb32 blue = new Rgb32(0, 0, 255, 255);
```
24 and 32 byte colors have no correlation with 48 and 64 bytes and cannot be casted with eachother.
```csharp
// 6 bytes for red, green and blue
Rgb48 Red = (Rgb48)0xffff0000;
Rgb48 Green = new Rgb48(0xff008000);
Rgb48 blue = new Rgb48(0, 0, 65535);
```
```csharp
// last 2 bytes represent opacity.
Rgb64 Red = (Rgb64)0xffff00000000;
Rgb64 Green = new Rgb64(0xff0080000000);
Rgb64 Blue = new Rgb64(0x00000000ffff);
```
For 24 and 32 bit types:
- `<Rgb32>` can be explicitly cast to `<Rgb24>`, but not vice versa.
- Both `<Rgb32>` and `<Rgb24>` types can be explicitly cast to `<uint>`.
- `<uint>` can be explicitly cast to both `<Rgb32>` and `<Rgb24>`.

For 48 and 64 bit types:

Both classes can be casted into each-other, and both classes can be casted into a `<uint>`.
- `<Rgb64>` can be explicitly cast to `<Rgb48>`, but not vice versa.
- Both `<Rgb64>` and `<Rgb48>` types can be explicitly cast to `<ulong>`.
- `<ulong>` can be explicitly cast to both `<Rgb64>` and `<Rgb48>`.

Note that casting from higher bit types to lower bit types result in a data loss.

```csharp
uint red = (uint)new Rgb24(255, 0, 0);
uint green = (uint)new Rgb32(0, 255, 0, 255);
```

Rgb24 blue = (Rgb24)new Rgb32(0, 0, 255, 255); // opacity will be lost.
```
```csharp
ulong red = (ulong)new Rgb48(65535, 0, 0);
ulong green = (ulong)new Rgb64(0, 65535, 0, 65535);

Rgb48 blue = (Rgb48)new Rgb64(0, 0, 65535, 65535); // opacity will be lost.
```

0 comments on commit bd1dae3

Please sign in to comment.