This code demonstrates how to draw a color wheel into a RGBA ImageView. The part outside the wheel is transparent, using alpha channel.
using System;
using System.Numerics;
using System.Runtime.Intrinsics;
{
public class ColorWheel
{
private static Vector3u8 HsvToRgb(
double h,
double s,
double v)
{
double hp = h / 60.0;
double c = s * v;
double x = c * (1 - Math.Abs((hp % 2) - 1));
double m = v - c;
double r = 0, g = 0, b = 0;
if (hp <= 1)
{
r = c;
g = x;
}
else if (hp <= 2)
{
r = x;
g = c;
}
else if (hp <= 3)
{
g = c;
b = x;
}
else if (hp <= 4)
{
g = x;
b = c;
}
else if (hp <= 5)
{
r = x;
b = c;
}
else
{
r = c;
b = x;
}
rgb_double *= 255.0;
return new Vector3u8((
byte)rgb_double[0], (
byte)rgb_double[1], (
byte)rgb_double[2]);
}
{
{
throw new ApplicationException("Shape must have 2 dimensions");
}
image.HasAlpha = true;
for (ulong y = 0; y < shape[1]; ++y)
{
for (ulong x = 0; x < shape[0]; ++x)
{
if (r > center[0])
{
byte[] pixVal = { 0, 0, 0, 0 };
image.
Write(positionPixel, pixVal);
}
else
{
double h = Math.Atan2(delta[1], delta[0]) * 180 / Math.PI + 180;
double s = r / center[0];
double v = 1;
var rgb = ColorWheel.HsvToRgb(h, s, v);
byte[] pixVal = { rgb[0], rgb[1], rgb[2], 255 };
image.
Write(positionPixel, pixVal);
}
}
}
return image;
}
public static void Main(string[] args)
{
Console.WriteLine(
"Generated image: " + image.
ToString());
Console.WriteLine("SUCCESS");
}
}
}
Stores information about a data type.
Definition DataType.cs:24
Enum for the image type (see ImageType)
Definition ImageTypeId.cs:20
This factory is aimed at creating dataset views.
Definition ImageViewFactory.cs:21
static ImageView Allocate(VectorXu64 shape, DataType type, ImageProperties properties, MetadataNode metadata)
Creates a memory image with the given shape and type.
Definition ImageViewFactory.cs:86
Interface representing an N dimensional image.
Definition ImageView.cs:30
unsafe void Write(VectorXu64 index, byte[] src)
Write value from a buffer at the given index.
Definition ImageView.cs:373
string ToString()
Return a string representation.
Definition ImageView.cs:226
An arithmetic vector.
Definition Vector2d.cs:29
double Length()
Returns the vector's norm.
Definition Vector2d.cs:259
An arithmetic vector.
Definition Vector3d.cs:29
static Vector3d CreateUniform(double value)
Create a vector with the given value for all components.
Definition Vector3d.cs:199
An arithmetic vector.
Definition Vector3u8.cs:29
A dynamically sized arithmetic vector.
Definition VectorXu64.cs:14
uint Size()
Returns the size of the vector, which also correspond to vector number of dimension.
Definition VectorXu64.cs:181
Definition CreateDataFrame.cs:6
Definition AccessCapabilities.cs:11
ImageInterpretation
Interpretation of an Image.
Definition ImageInterpretation.cs:15
DataTypeId
A collection of built-in data types.
Definition DataTypeId.cs:15