Table of Contents

Class ColorUtil

Namespace
Vintagestory.API.MathTools
Assembly
VintagestoryAPI.dll

Many utility methods and fields for working with colors

public class ColorUtil
Inheritance
ColorUtil
Inherited Members
Extension Methods

Fields

BlackArgb

Black opaque rgb color

public static readonly int BlackArgb

Field Value

int

BlackArgbDouble

White opaque color as normalized float values (0..1)

public static readonly double[] BlackArgbDouble

Field Value

double[]

BlackArgbVec

Black opaque rgb color

public static readonly Vec4f BlackArgbVec

Field Value

Vec4f

BlackRgbVec

Black opaque rgb color

public static readonly Vec3f BlackRgbVec

Field Value

Vec3f

BrightBits

Amount of bits per block that are available to store the brightness value

public const int BrightBits = 5

Field Value

int

BrightMul

public const int BrightMul = 8

Field Value

int

BrightQuantities

public static int BrightQuantities

Field Value

int

ClearAlpha

~(255 << 24)

public const int ClearAlpha = 16777215

Field Value

int

HueBits

Amount of bits per block that are available to store the hue value

public const int HueBits = 6

Field Value

int

HueMul

public const int HueMul = 4

Field Value

int

HueQuantities

public static int HueQuantities

Field Value

int

OpaqueAlpha

255 << 24

public const int OpaqueAlpha = -16777216

Field Value

int

SatBits

Amount of bits per block that are available to store the saturation value

public const int SatBits = 3

Field Value

int

SatMul

public const int SatMul = 32

Field Value

int

SatQuantities

public static int SatQuantities

Field Value

int

WhiteAhsl

White opaque AHSV color

public static readonly int WhiteAhsl

Field Value

int

WhiteAhsvBytes

White opaque ahsv color as bytes (0..255)

public static readonly byte[] WhiteAhsvBytes

Field Value

byte[]

WhiteArgb

White opaque argb color

public const int WhiteArgb = -1

Field Value

int

WhiteArgbBytes

White opaque argb color as bytes (0..255)

public static readonly byte[] WhiteArgbBytes

Field Value

byte[]

WhiteArgbDouble

White opaque color as normalized float values (0..1)

public static readonly double[] WhiteArgbDouble

Field Value

double[]

WhiteArgbFloat

White opaque color as normalized float values (0..1)

public static readonly float[] WhiteArgbFloat

Field Value

float[]

WhiteArgbVec

White opaque color as normalized float values (0..1)

public static readonly Vec4f WhiteArgbVec

Field Value

Vec4f

WhiteRgbVec

White opaque color as normalized float values (0..1)

public static readonly Vec3f WhiteRgbVec

Field Value

Vec3f

Methods

ColorA(int)

Returns alpha value of given color

public static byte ColorA(int color)

Parameters

color int

Returns

byte

ColorAverage(int[], float[])

Averages several colors together in RGB space

public static int ColorAverage(int[] colors, float[] weights)

Parameters

colors int[]
weights float[]

Returns

int

ColorB(int)

Returns blue value of given color

public static byte ColorB(int color)

Parameters

color int

Returns

byte

ColorCombineHSV(int, int, int, int, int, int)

Combines two HSV colors by converting them to rgb then back to hsv. Uses the brightness as a weighting factor. Also leaves the brightness at the max of both hsv colors.

public static int[] ColorCombineHSV(int h1, int s1, int v1, int h2, int s2, int v2)

Parameters

h1 int
s1 int
v1 int
h2 int
s2 int
v2 int

Returns

int[]

Combined HSV Color

ColorFromRgba(byte[])

public static int ColorFromRgba(byte[] channels)

Parameters

channels byte[]

Returns

int

ColorFromRgba(double[])

public static int ColorFromRgba(double[] col)

Parameters

col double[]

Returns

int

ColorFromRgba(int, int, int, int)

Care: the returned value is in true RGBA order, not BGRA as used for example by VS particle system. Therefore, depending on use, calling code may need to exchange the r and b parameters to see correct colors rendered in-game.

public static int ColorFromRgba(int r, int g, int b, int a)

Parameters

r int
g int
b int
a int

Returns

int

ColorFromRgba(Vec4f)

public static int ColorFromRgba(Vec4f colorRel)

Parameters

colorRel Vec4f

Returns

int

ColorG(int)

Returns green value of given color

public static byte ColorG(int color)

Parameters

color int

Returns

byte

ColorMultiply(byte[], byte[])

Multiplies two colors together: c=(a*b)/255

public static byte[] ColorMultiply(byte[] color1, byte[] color2)

Parameters

color1 byte[]
color2 byte[]

Returns

byte[]

ColorMultiply3(int, float)

Multiplies a float value to the rgb color channels, leaves alpha channel unchanged

public static int ColorMultiply3(int color, float multiplier)

Parameters

color int
multiplier float

Returns

int

ColorMultiply3Clamped(int, float)

Multiplies a float value to the rgb color channels, leaves alpha channel unchanged. Makes sure the multiplied value stays within the 0..255 range

public static int ColorMultiply3Clamped(int color, float multiplier)

Parameters

color int
multiplier float

Returns

int

ColorMultiply4(int, float)

Multiplies a float value to every color channel including the alpha component.

public static int ColorMultiply4(int color, float multiplier)

Parameters

color int
multiplier float

Returns

int

ColorMultiply4(int, float, float, float, float)

Multiplies a float value to the rgb color channels

public static int ColorMultiply4(int color, float redMul, float greenMul, float blueMul, float alphaMul)

Parameters

color int
redMul float
greenMul float
blueMul float
alphaMul float

Returns

int

ColorMultiplyEach(int, int)

Multiplies two colors together c=(a*b)/255

public static int ColorMultiplyEach(int color, int color2)

Parameters

color int
color2 int

Returns

int

ColorOver(int, int)

Overlays rgb1 on top of rgb2, based on their alpha values

public static int ColorOver(int rgb1, int rgb2)

Parameters

rgb1 int
rgb2 int

Returns

int

ColorOverlay(int, int, float)

Overlays rgb2 over rgb1 When c2weight = 0 resulting color is color1, when c2weight = 1 then resulting color is color2 Resulting color alpha value is 100% color1 alpha

public static int ColorOverlay(int rgb1, int rgb2, float c2weight)

Parameters

rgb1 int
rgb2 int
c2weight float

Returns

int

ColorR(int)

Returns red value of given color

public static byte ColorR(int color)

Parameters

color int

Returns

byte

ColorSubstractHSV(int, int, int, int, int, int)

Removes HSV2 from HSV1 by converting them to rgb then back to hsv. Uses the brightness as a weighting factor. Leaves brightness unchanged.

public static int[] ColorSubstractHSV(int h1, int s1, int v1, int h2, int s2, int v2)

Parameters

h1 int
s1 int
v1 int
h2 int
s2 int
v2 int

Returns

int[]

ColorToString(int)

Returns human a readable string of given color

public static string ColorToString(int color)

Parameters

color int

Returns

string

Doubles2Hex(double[])

public static string Doubles2Hex(double[] color)

Parameters

color double[]

Returns

string

FromRGBADoubles(double[])

public static int FromRGBADoubles(double[] rgba)

Parameters

rgba double[]

Returns

int

GetIncandescenceColorAsColor4f(int)

public static float[] GetIncandescenceColorAsColor4f(int temperature)

Parameters

temperature int

Returns

float[]

GrayscaleColor(byte)

Returns a fully opaque gray color with given brightness

public static int GrayscaleColor(byte brightness)

Parameters

brightness byte

Returns

int

HSVa2RGBaBytes(byte[])

Converts given HSVA values into its respective RGBA Representation (all values in range of 0-255) R is the first byte in the resulting array

public static byte[] HSVa2RGBaBytes(byte[] hsva)

Parameters

hsva byte[]

Returns

byte[]

Hex2Doubles(string)

Parses a hex string as an rgb(a) color and returns an array of colors normalized fom 0..1 for use with Cairo. E.g. turns #FF0000 into double[1, 0, 0, 1] and #00FF00CC into double[0, 1, 0, 0.8]

public static double[] Hex2Doubles(string hex)

Parameters

hex string

Returns

double[]

Hex2Doubles(string, double)

public static double[] Hex2Doubles(string hex, double opacityRel)

Parameters

hex string
opacityRel double

Returns

double[]

Hex2Int(string)

Turn a string hex color (with #) into a single int

public static int Hex2Int(string hex)

Parameters

hex string

Returns

int

Hsv2Rgb(int)

Converts given HSV value into it's respective RGB Representation (all values in range of 0-255) R is the most significant byte i.e. this is RGB

public static int Hsv2Rgb(int hsv)

Parameters

hsv int

Returns

int

Hsv2RgbInts(int, int, int)

Converts given HSV values into its respective RGB representation (all values in range of 0-255) R is the first byte in the resulting array

public static int[] Hsv2RgbInts(int h, int s, int v)

Parameters

h int
s int
v int

Returns

int[]

HsvToRgb(int, int, int)

Converts given HSV values into it's respective RGB Representation (all values in range of 0-255) R is the most significant byte i.e. this is RGB

public static int HsvToRgb(int h, int s, int v)

Parameters

h int
s int
v int

Returns

int

HsvToRgba(int, int, int)

Converts given HSB values into it's respective ARGB Representation (all values in range of 0-255, alpha always 255) R is the LEAST significant byte i.e. the result is BGR

public static int HsvToRgba(int h, int s, int v)

Parameters

h int
s int
v int

Returns

int

HsvToRgba(int, int, int, int)

Converts given HSV values into its respective ARGB Representation (all values in range of 0-255) R is the LEAST significant byte i.e. the result is BGR

public static int HsvToRgba(int h, int s, int v, int a)

Parameters

h int
s int
v int
a int

Returns

int

Int2Hex(int)

Turn a color int into its string hex version, including preceeding #

public static string Int2Hex(int color)

Parameters

color int

Returns

string

Int2HexBGR(int)

public static string Int2HexBGR(int color)

Parameters

color int

Returns

string

Int2HexRgba(int)

Turn a color int into its string hex version, including preceeding #, including alpha channel

public static string Int2HexRgba(int color)

Parameters

color int

Returns

string

ReverseColorBytes(byte[])

Reverses the RGB channels, but leaves alpha untouched. Basically turns RGBA into BGRA and vice versa

public static byte[] ReverseColorBytes(byte[] color)

Parameters

color byte[]

Returns

byte[]

ReverseColorBytes(int)

Reverses the RGB channels, but leaves alpha untouched. Basically turns RGBA into BGRA and vice versa

public static int ReverseColorBytes(int color)

Parameters

color int

Returns

int

Rgb2HSv(int)

Converts given RGB value into it's respective HSV Representation (all values in range of 0-255) In the parameter, R is the most significant byte i.e. this is for RGB In the result, V is the LEAST significant byte

public static int Rgb2HSv(int rgb)

Parameters

rgb int

Returns

int

Rgb2Hsv(float, float, float)

Converts given RGB values into it's respective HSV Representation (all values in range of 0-255) In the result, V is the MOST significant byte

public static int Rgb2Hsv(float r, float g, float b)

Parameters

r float
g float
b float

Returns

int

RgbToHsvInts(int, int, int)

Converts given RGB values into it's respective HSV Representation (all values in range of 0-255)

public static int[] RgbToHsvInts(int r, int g, int b)

Parameters

r int
g int
b int

Returns

int[]

ToBGRABytes(int)

Splits up a 32bit int color into 4 1 byte components, in BGRA order (Alpha channel at the highest 8 bits)

public static byte[] ToBGRABytes(int color)

Parameters

color int

Returns

byte[]

ToRGBABytes(int)

Splits up a 32bit int color into 4 1 byte components, in RGBA order

public static byte[] ToRGBABytes(int color)

Parameters

color int

Returns

byte[]

ToRGBADoubles(int)

Returns a 4 element rgb double with values between 0..1

public static double[] ToRGBADoubles(int color)

Parameters

color int

Returns

double[]

ToRGBAFloats(int)

Returns a 4 element rgb float with values between 0..1

public static float[] ToRGBAFloats(int color)

Parameters

color int

Returns

float[]

ToRGBAVec4f(int)

public static Vec4f ToRGBAVec4f(int color)

Parameters

color int

Returns

Vec4f

ToRGBAVec4f(int, ref Vec4f)

public static Vec4f ToRGBAVec4f(int color, ref Vec4f outVal)

Parameters

color int
outVal Vec4f

Returns

Vec4f

ToRGBVec3f(int, ref Vec3f)

Returns a 4 element rgb float with values between 0..1

public static Vec3f ToRGBVec3f(int color, ref Vec3f outVal)

Parameters

color int
outVal Vec3f

Returns

Vec3f

ToRgba(int, int, int, int)

Pack the 4 color components into a single ARGB 32bit int

public static int ToRgba(int a, int r, int g, int b)

Parameters

a int
r int
g int
b int

Returns

int

getIncandescenceColor(int)

public static int[] getIncandescenceColor(int temperature)

Parameters

temperature int

Returns

int[]