The OpenD Programming Language

std.experimental.color

This package defines human-visible colors in various formats.

RGB color formats are particularly flexible to express typical RGB image data in a wide variety of common formats without having to write adapters.

It is intended that this library facilitate a common API that allows a variety of image and multimedia libraries to interact more seamlessly, without need for constant conversions between custom, library-defined color data types.

This package pays very careful attention to correctness with respect to color space definitions, and correct handling of color space conversions. For best results, users should also pay careful attention to color space selection when working with color data, and the rest will follow. A crash course on understanding color space can be found at wikipedia.

More information regarding specific color spaces can be found in their respective modules.

All types and functions offered in this package are pure, nothrow, @safe and @nogc. It is intended to be useful by realtime or memory-contrained systems such as video games, games consoles or mobile devices.

More...

Modules

colorspace
module std.experimental.color.colorspace

This module defines and operates on standard color spaces.

hsx
module std.experimental.color.hsx

This module implements HSV, HSL, HSI, HCY, HWB, HCG _color types.

lab
module std.experimental.color.lab

This module implements the CIE Lab and LCh _color types.

packedrgb
module std.experimental.color.packedrgb

This module implements the packed RGB _color type.

rgb
module std.experimental.color.rgb

This module implements the RGB _color type.

xyz
module std.experimental.color.xyz

This module implements CIE XYZ and xyY _color types.

Members

Aliases

A8
alias A8 = RGB!("a", ubyte)

8 bit alpha-only color type.

BGR8
alias BGR8 = RGB!("bgr", ubyte)

24 bit BGR color type with 8 bits per channel.

BGRA8
alias BGRA8 = RGB!("bgra", ubyte)

32 bit BGR + alpha color type with 8 bits per channel.

BGRX8
alias BGRX8 = RGB!("bgrx", ubyte)

32 bit BGR color type with 8 bits per channel.

L8
alias L8 = RGB!("l", ubyte)

8 bit luminance-only color type.

LA8
alias LA8 = RGB!("la", ubyte)

16 bit luminance + alpha color type with 8 bits per channel.

RGB8
alias RGB8 = RGB!("rgb", ubyte)

24 bit RGB color type with 8 bits per channel.

RGBA8
alias RGBA8 = RGB!("rgba", ubyte)

32 bit RGB + alpha color type with 8 bits per channel.

RGBAf32
alias RGBAf32 = RGB!("rgba", float)

Floating point RGB + alpha color type.

RGBX8
alias RGBX8 = RGB!("rgbx", ubyte)

32 bit RGB color type with 8 bits per channel.

RGBf32
alias RGBf32 = RGB!("rgb", float)

Floating point RGB color type.

UV8
alias UV8 = RGB!("rg", byte)

16 bit signed UV color type with 8 bits per channel.

UVW8
alias UVW8 = RGB!("rgb", byte)

24 bit signed UVW color type with 8 bits per channel.

Enums

Colors
enum Colors

Set of colors defined by X11, adopted by the W3C, SVG, and other popular libraries.

isColor
eponymoustemplate isColor(T)

Detect whether T is a color type compatible with std.experimental.color.

isColorComponentType
eponymoustemplate isColorComponentType(T)

Detect whether T is a valid color component type.

isColorScalarType
eponymoustemplate isColorScalarType(T)

Detect whether T can represent a color component.

Functions

colorFromString
Color colorFromString(const(char)[] str)

Create a color from a string.

colorFromString
bool colorFromString(const(char)[] str, Color color)

Create a color from a string.

convertColor
To convertColor(From color)

Convert between color types.

Detailed Description

Expressing images:

Images may be expressed in a variety of ways, but a simple way may be to use std.experimental.ndslice to produce simple n-dimensional images.

import std.experimental.color;
import std.experimental.ndslice;

auto imageBuffer = new RGB8[height*width];
auto image = imageBuffer.sliced(height, width);

foreach(ref row; image)
{
    foreach(ref pixel; row)
    {
        pixel = Colors.white;
    }
}

Use of ndslice this way allows the use of n-dimentional slices to produce sub-images.

Implement custom color type:

The library is extensible such that users or libraries can easily supply their own custom color formats and expect comprehensive conversion and interaction with any other libraries or code that makes use of std.experimental.color.

The requirement for a user color type is to specify a 'parent' color space, and expose at least a set of conversion functions to/from that parent.

For instance, HSV is a cylindrical representation of RGB colors, so the 'parent' color type in this case is said to be RGB. If your custom color space is not derivative of an existing color space, then you should provide conversion between CIE XYZ, which can most simply express all of human-visible color.

struct HueOnlyColor
{
    alias ParentColor = HSV!float;

    static To convertColorImpl(To, From)(From color) if (is(From == HueOnlyColor) && isHSx!To)
    {
        return To(color.hue, 1.0, 1.0); // assume maximum saturation, maximum lightness
    }

    static To convertColorImpl(To, From)(From color) if (isHSx!From && is(To == HueOnlyColor))
    {
        return HueOnlyColor(color.h); // just keep the hue
    }

private:
    float hue;
}

static assert(isColor!HueOnlyColor == true, "This is all that is required to create a valid color type");

If your color type has template args, it may also be necessary to produce a third convertColorImpl function that converts between instantiations with different template args.

Meta

Authors

Manu Evans