Replace QColor and QColorF with OpenTK.Mathematics.Color4.

This commit is contained in:
H. Utku Maden 2024-07-28 13:59:35 +03:00
parent 42782b8a71
commit 831c93b916
8 changed files with 61 additions and 160 deletions

@ -4,112 +4,6 @@ using System.Diagnostics;
namespace Dashboard
{
/// <summary>
/// A RGBA color value.
/// </summary>
[DebuggerDisplay("({R}, {G}, {B}, {A})")]
public struct QColor
{
/// <summary>
/// Red channel.
/// </summary>
public byte R;
/// <summary>
/// Green channel.
/// </summary>
public byte G;
/// <summary>
/// Blue channel.
/// </summary>
public byte B;
/// <summary>
/// Alpha channel.
/// </summary>
public byte A;
public QColor(byte r, byte g, byte b, byte a)
{
R = r;
G = g;
B = b;
A = a;
}
public QColor(byte r, byte g, byte b) : this(r, g, b, 1) { }
public QColor(uint hexCode)
{
R = (byte)((hexCode >> 24) & 0xFF);
G = (byte)((hexCode >> 16) & 0xFF);
B = (byte)((hexCode >> 8 ) & 0xFF);
A = (byte)((hexCode >> 0 ) & 0xFF);
}
public QColor(int hexCode) : this((uint)hexCode) { }
public static readonly QColor Black = new QColor(0, 0, 0, 255);
public static readonly QColor Red = new QColor(255, 0, 0, 255);
public static readonly QColor Green = new QColor(0, 255, 0, 255);
public static readonly QColor Blue = new QColor(0, 0, 255, 255);
public static readonly QColor Yellow = new QColor(255, 255, 0, 255);
public static readonly QColor Cyan = new QColor(0, 255, 255, 255);
public static readonly QColor Magenta = new QColor(255, 0, 255, 255);
public static readonly QColor White = new QColor(255, 255, 255, 255);
public static explicit operator QColorF(QColor a)
{
return new QColorF(a.R/255.0f, a.G/255.0f, a.B/255.0f, a.A/255.0f);
}
}
public struct QColorF
{
/// <summary>
/// Red channel.
/// </summary>
public float R;
/// <summary>
/// Green channel.
/// </summary>
public float G;
/// <summary>
/// Blue channel.
/// </summary>
public float B;
/// <summary>
/// Alpha channel.
/// </summary>
public float A;
public QColorF(float r, float g, float b, float a)
{
R = r; G = g; B = b; A = a;
}
public QColorF(float r, float g, float b) : this(r, g, b, 1.0f) { }
public QColorF(uint hexCode)
{
R = ((hexCode >> 24) & 0xFF)/255.0f;
G = ((hexCode >> 16) & 0xFF)/255.0f;
B = ((hexCode >> 8 ) & 0xFF)/255.0f;
A = ((hexCode >> 0 ) & 0xFF)/255.0f;
}
public QColorF(int hexCode) : this((uint)hexCode) { }
public static readonly QColorF Black = new QColorF(0, 0, 0, 1.0f);
public static readonly QColorF Red = new QColorF(1.0f, 0, 0, 1.0f);
public static readonly QColorF Green = new QColorF(0, 1, 0, 1);
public static readonly QColorF Blue = new QColorF(0, 0, 1, 1);
public static readonly QColorF Yellow = new QColorF(1, 1, 0, 1);
public static readonly QColorF Cyan = new QColorF(0, 1, 1, 1);
public static readonly QColorF Magenta = new QColorF(1, 0, 1, 1);
public static readonly QColorF White = new QColorF(1, 1, 1, 1);
public static explicit operator QColor(QColorF a)
{
return new QColor((byte)(a.R * 255), (byte)(a.G * 255), (byte)(a.B * 255), (byte)(a.A * 255));
}
}
/// <summary>
/// A bezier curve segment.
/// </summary>

@ -299,12 +299,17 @@ namespace Dashboard.ImmediateDraw
}
}
public static explicit operator QColor(in Frame frame)
public static explicit operator Color4(in Frame frame)
{
if (frame.Type != FrameType.IVec4)
switch (frame.Type)
{
case FrameType.IVec4:
return new Color4((byte)frame._i1, (byte)frame._i2, (byte)frame._i3, (byte)frame._i4);
case FrameType.Vec4:
return new Color4(frame._f1, frame._f2, frame._f3, frame._f4);
default:
throw new InvalidCastException();
return new QColor((byte)frame._i1, (byte)frame._i2, (byte)frame._i3, (byte)frame._i4);
}
}
public static explicit operator QRectangle(in Frame frame)
@ -338,7 +343,7 @@ namespace Dashboard.ImmediateDraw
public static explicit operator Frame(float f) => new Frame(f);
public static implicit operator Frame(Command cmd) => new Frame(cmd);
public static implicit operator Frame(in Vector2 vector) => new Frame(vector.X, vector.Y);
public static implicit operator Frame(in QColor color) => new Frame(color.R, color.G, color.B, color.A);
public static implicit operator Frame(in Color4 color) => new Frame(color.R, color.G, color.B, color.A);
public static implicit operator Frame(in QRectangle rect) => new Frame(rect.Max.X, rect.Max.Y, rect.Min.X, rect.Min.Y);
public static implicit operator Frame(in QLine line) => new Frame(line.Start.X, line.Start.Y, line.End.X, line.Start.Y);

@ -1,4 +1,5 @@
using System;
using OpenTK.Mathematics;
namespace Dashboard.Media.Color
{
@ -27,7 +28,7 @@ namespace Dashboard.Media.Color
for (int i = 0; i < count; i++)
{
QColorF color = io[i];
Color4 color = io[i];
color.R *= color.A;
color.G *= color.A;
@ -42,8 +43,8 @@ namespace Dashboard.Media.Color
for (int i = 0; i < count; i++)
{
QColor color = io[i];
float a = color.A/255.0f;
Color4 color = io[i];
float a = color.A;
color.R = (byte)(color.R * a);
color.G = (byte)(color.G * a);
@ -75,7 +76,7 @@ namespace Dashboard.Media.Color
int count = dst.Width * dst.Height * dst.Depth;
for (int i = 0; i < count; i++)
{
dstIO[i] = (QColor)srcIO[i];
dstIO[i] = srcIO[i];
}
}
else if (dst.Format.IsFloat() && src.Format.IsU8())
@ -86,7 +87,7 @@ namespace Dashboard.Media.Color
int count = dst.Width * dst.Height * dst.Depth;
for (int i = 0; i < count; i++)
{
dstIO[i] = (QColorF)srcIO[i];
dstIO[i] = srcIO[i];
}
}
else if (dst.Format.IsFloat() && src.Format.IsFloat())

@ -1,4 +1,5 @@
using System;
using OpenTK.Mathematics;
namespace Dashboard.Media.Color
{
@ -18,7 +19,7 @@ namespace Dashboard.Media.Color
Lock = imageLock;
}
public QColor this[int index]
public Color4 this[int index]
{
get
{
@ -28,11 +29,11 @@ namespace Dashboard.Media.Color
switch (Format)
{
default:
case QImageFormat.RedU8: return new QColor(ptr[0], 0, 0, 255);
case QImageFormat.AlphaU8: return new QColor(0, 0, 0, ptr[0]);
case QImageFormat.RaU8: return new QColor(ptr[0], 0, 0, ptr[1]);
case QImageFormat.RgbU8: return new QColor(ptr[0], ptr[1], ptr[2], 255);
case QImageFormat.RgbaU8: return new QColor(ptr[0], ptr[1], ptr[2], ptr[3]);
case QImageFormat.RedU8: return new Color4(ptr[0], 0, 0, 255);
case QImageFormat.AlphaU8: return new Color4(0, 0, 0, ptr[0]);
case QImageFormat.RaU8: return new Color4(ptr[0], 0, 0, ptr[1]);
case QImageFormat.RgbU8: return new Color4(ptr[0], ptr[1], ptr[2], 255);
case QImageFormat.RgbaU8: return new Color4(ptr[0], ptr[1], ptr[2], ptr[3]);
}
}
@ -45,30 +46,30 @@ namespace Dashboard.Media.Color
{
default:
case QImageFormat.RedU8:
ptr[0] = value.R;
ptr[0] = (byte)(value.R * 255);
break;
case QImageFormat.AlphaU8:
ptr[0] = value.A;
ptr[0] = (byte)(value.A * 255);
break;
case QImageFormat.RaU8:
ptr[0] = value.R;
ptr[1] = value.A;
ptr[0] = (byte)(value.R * 255);
ptr[1] = (byte)(value.A * 255);
break;
case QImageFormat.RgbU8:
ptr[0] = value.R;
ptr[1] = value.G;
ptr[2] = value.B;
ptr[0] = (byte)(value.R * 255);
ptr[1] = (byte)(value.G * 255);
ptr[2] = (byte)(value.B * 255);
break;
case QImageFormat.RgbaU8:
ptr[0] = value.R;
ptr[1] = value.G;
ptr[2] = value.B;
ptr[3] = value.A;
ptr[0] = (byte)(value.R * 255);
ptr[1] = (byte)(value.G * 255);
ptr[2] = (byte)(value.B * 255);
ptr[3] = (byte)(value.A * 255);
break;
}
}
}
public QColor this[int x, int y, int z = 0]
public Color4 this[int x, int y, int z = 0]
{
get => this[x + y * Width + z * Width * Height];
set => this[x + y * Width + z * Width * Height] = value;
@ -91,7 +92,7 @@ namespace Dashboard.Media.Color
Lock = imageLock;
}
public QColorF this[int index]
public Color4 this[int index]
{
get
{
@ -101,11 +102,11 @@ namespace Dashboard.Media.Color
switch (Format)
{
default:
case QImageFormat.RedU8: return new QColorF(ptr[0], 0, 0, 255);
case QImageFormat.AlphaU8: return new QColorF(0, 0, 0, ptr[0]);
case QImageFormat.RaU8: return new QColorF(ptr[0], 0, 0, ptr[1]);
case QImageFormat.RgbU8: return new QColorF(ptr[0], ptr[1], ptr[2], 255);
case QImageFormat.RgbaU8: return new QColorF(ptr[0], ptr[1], ptr[2], ptr[3]);
case QImageFormat.RedU8: return new Color4(ptr[0], 0, 0, 1);
case QImageFormat.AlphaU8: return new Color4(0, 0, 0, ptr[0]);
case QImageFormat.RaU8: return new Color4(ptr[0], 0, 0, ptr[1]);
case QImageFormat.RgbU8: return new Color4(ptr[0], ptr[1], ptr[2], 1);
case QImageFormat.RgbaU8: return new Color4(ptr[0], ptr[1], ptr[2], ptr[3]);
}
}
@ -141,7 +142,7 @@ namespace Dashboard.Media.Color
}
}
}
public QColorF this[int x, int y, int z = 0]
public Color4 this[int x, int y, int z = 0]
{
get => this[x + y * Width + z * Width * Height];
set => this[x + y * Width + z * Width * Height] = value;

@ -296,7 +296,7 @@ namespace Dashboard.OpenGL
GL.VertexAttribPointer(driver.fZIndex, 1, VertexAttribPointerType.UnsignedInt, false, DbVertex.Stride, DbVertex.ZIndexOffset);
GL.VertexAttribPointer(driver.v2TexPos, 2, VertexAttribPointerType.Float, false, DbVertex.Stride, DbVertex.TextureCoordinatesOffset);
GL.VertexAttribPointer(driver.fTexLayer, 1, VertexAttribPointerType.Float, false, DbVertex.Stride, DbVertex.TextureLayerOffset);
GL.VertexAttribPointer(driver.v4Color, 4, VertexAttribPointerType.UnsignedByte, true, DbVertex.Stride, DbVertex.ColorOffset);
GL.VertexAttribPointer(driver.v4Color, 4, VertexAttribPointerType.Float, true, DbVertex.Stride, DbVertex.ColorOffset);
GL.EnableVertexAttribArray(driver.v2Position);
GL.EnableVertexAttribArray(driver.fZIndex);
GL.EnableVertexAttribArray(driver.v2TexPos);

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Dashboard.Media;
using Dashboard.Media.Font;
using OpenTK.Mathematics;
namespace Dashboard
{
@ -55,9 +56,9 @@ namespace Dashboard
{
public abstract object? this[string key] { get; set; }
public QColor? Color
public Color4? Color
{
get => (QColor?)this["color"];
get => (Color4?)this["color"];
set => this["color"] = value;
}
@ -121,9 +122,9 @@ namespace Dashboard
set => this["stroke-width"] = value;
}
public QColor? StrokeColor
public OpenTK.Mathematics.Color4? StrokeColor
{
get => (QColor?)this["stroke-color"];
get => (Color4?)this["stroke-color"];
set => this["stroke-color"] = value;
}
@ -237,7 +238,7 @@ namespace Dashboard
/// <summary>
/// Stroke color.
/// </summary>
public QColor Color { get; set; }
public Color4 Color { get; set; }
/// <summary>
/// Stroke width.
@ -253,7 +254,7 @@ namespace Dashboard
{
}
public QuikStrokeStyle(QColor color, float width /*, QuikStipplePattern pattern*/)
public QuikStrokeStyle(Color4 color, float width /*, QuikStipplePattern pattern*/)
{
Color = color;
Width = width;
@ -270,6 +271,6 @@ namespace Dashboard
/// </summary>
public class QuikFillStyle
{
public QColor Color { get; set; }
public Color4 Color { get; set; }
}
}

@ -22,7 +22,7 @@ namespace Dashboard.VertexGenerator
/// <summary>
/// Per vertex color value.
/// </summary>
public QColor Color;
public Color4 Color;
/// <summary>
/// Per vertex depth index value.
@ -37,7 +37,7 @@ namespace Dashboard.VertexGenerator
public static int PositionOffset => 0;
public static unsafe int TextureCoordinatesOffset => sizeof(Vector2);
public static unsafe int ColorOffset => 2 * sizeof(Vector2);
public static unsafe int ZIndexOffset => ColorOffset + sizeof(QColor);
public static unsafe int ZIndexOffset => ColorOffset + sizeof(Color4);
public static unsafe int TextureLayerOffset => ZIndexOffset + sizeof(int);
public static unsafe int Stride => sizeof(DbVertex);
}

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using Dashboard.ImmediateDraw;
using Dashboard.Media;
using Dashboard.Typography;
using OpenTK.Mathematics;
namespace Dashboard.VertexGenerator
@ -22,18 +21,18 @@ namespace Dashboard.VertexGenerator
protected DbVertex StrokeVertex => new DbVertex()
{
ZIndex = Style.ZIndex ?? this.ZIndex,
Color = Style.StrokeColor ?? QColor.Black,
Color = Style.StrokeColor ?? Color4.Black,
};
protected DbVertex FillVertex => new DbVertex()
{
ZIndex = Style.ZIndex ?? this.ZIndex,
Color = Style.Color ?? QColor.White,
Color = Style.Color ?? Color4.White,
};
protected DbVertex ImageVertex => new DbVertex()
{
ZIndex = Style.ZIndex ?? this.ZIndex,
Color = BlendTextures ? (Style.Color ?? QColor.White) : QColor.White,
Color = BlendTextures ? (Style.Color ?? Color4.White) : Color4.White,
};
public override void Reset()
@ -42,7 +41,7 @@ namespace Dashboard.VertexGenerator
DrawQueue.Clear();
}
protected override void ChildProcessCommand(Command name, ImmediateDraw.DrawQueue queue)
protected override void ChildProcessCommand(Command name, DrawQueue queue)
{
base.ChildProcessCommand(name, queue);
@ -68,7 +67,7 @@ namespace Dashboard.VertexGenerator
}
private readonly List<QLine> LineList = new List<QLine>();
private void LineProc(ImmediateDraw.DrawQueue queue)
private void LineProc(DrawQueue queue)
{
Frame frame = queue.Dequeue();
@ -410,7 +409,7 @@ namespace Dashboard.VertexGenerator
}
private readonly List<QRectangle> RectangleList = new List<QRectangle>();
private void RectangleProc(ImmediateDraw.DrawQueue queue)
private void RectangleProc(DrawQueue queue)
{
Frame frame = queue.Dequeue();
RectangleList.Clear();
@ -1013,7 +1012,7 @@ namespace Dashboard.VertexGenerator
DrawQueue.AddElement(s1); DrawQueue.AddElement(s2); DrawQueue.AddElement(4);
}
private void ImageProc(ImmediateDraw.DrawQueue queue)
private void ImageProc(DrawQueue queue)
{
Frame frame = queue.Dequeue();
ImageCommandFlags flags = (ImageCommandFlags)frame.I1;
@ -1030,7 +1029,7 @@ namespace Dashboard.VertexGenerator
}
}
private void Image2d(ImmediateDraw.DrawQueue queue, QImage image, int count, bool uv)
private void Image2d(DrawQueue queue, QImage image, int count, bool uv)
{
DrawQueue.StartDrawCall(Viewport, image);