NAP
Public Types | Public Member Functions | Static Public Member Functions | List of all members
BaseColor Class Referenceabstract

#include <color.h>

Public Types

using Converter = std::function< void(const BaseColor &, BaseColor &, int)>
 

Public Member Functions

 BaseColor (int channels, int size)
 
virtual ~BaseColor ()=default
 
bool operator== (const BaseColor &rhs)=delete
 
bool operator!= (const BaseColor &rhs)=delete
 
int getNumberOfChannels () const
 
int valueSize () const
 
virtual rtti::TypeInfo getValueType () const =0
 
virtual bool isPointer () const =0
 
void convert (BaseColor &target) const
 
Converter getConverter (const BaseColor &target) const
 
template<typename T >
convert () const
 
virtual const void * getData (int channel) const =0
 
virtual void * getData (int channel)=0
 
int size () const
 

Static Public Member Functions

static void convertColor (const BaseColor &source, BaseColor &target)
 
static void convertColor (const BaseColor &source, BaseColor &target, const Converter &converter)
 
static Converter getConverter (const BaseColor &source, const BaseColor &target)
 

Description

Base class for all types of color. There are two types of colors: Colors that manage their own values and colors that point to values in memory. Colors that point to values in memory have the suffix Data, where colors that manage their own values don't: ie: RGBColor8 is a color that holds 3 RGB uint8 (Byte) color values. RGBColorData8 on the other hand holds 3 pointers to RGB uint8 (Byte) values and does not own the data it points to. Colors that point to values in memory are useful when working with bitmaps and a copy is often unnecessary. They're also very useful to change the color value of a pixel in a bitmap directly, look at conversion for more info.

Inheritance diagram for BaseColor:
[legend]

Member Typedef Documentation

◆ Converter

using Converter = std::function<void(const BaseColor&, BaseColor&, int)>

converts color type A in to type B

Constructor & Destructor Documentation

◆ BaseColor()

BaseColor ( int  channels,
int  size 
)

Base constructor associated with a color

Parameters
channelsthe number of channels of the color
sizethe size in bytes of a single color channel

◆ ~BaseColor()

virtual ~BaseColor ( )
virtualdefault

Member Function Documentation

◆ convert() [1/2]

T convert

Returns this color as a color of type T. Channels that can't be converted are initialized to 0. When converting to and from float colors, normalized color values are used. Float values that do not fall within the 0-1 range are clamped This call won't work with colors that point to values in memory! Valid options for T are: RGBColor8, RColor8, RGBColorFloat etc.

Example:

// Create 8 bit RGB color
RGBColor8 eight_bit_color = { 0xC8, 0x69, 0x69 };
// Convert to RGBA float color
// The RGB channels are converted. Alpha is initialized to 0.
RGBAColorFloat as_float_color = eight_bit_color.convert<RGBAColorFloat>();
Returns
the converted color

◆ convert() [2/2]

void convert ( BaseColor target) const

Converts and copies the values associated with this color in to the target color. Channels that can't be converted are initialized to 0. When converting to and from float colors, normalized color values are used. Float values that do not fall within the 0-1 range are clamped. When the target does not manage it's own color values, ie: holds pointers to color values in memory, the values that are pointed to in target are overridden. This makes it possible (for example) to write colors directly in to a bitmap without having to copy them over.

This is therefore valid (source)RGBAColorData8 -> (target)RGBColor8, the target will have a copy of the values pointed to by it's source The following is also valid: (source)RGBColor8 -> (target)RGBColorData8, the target will have the values it points to replaced by the converted color value

Parameters
targetthe converted color value

◆ convertColor() [1/2]

static void convertColor ( const BaseColor source,
BaseColor target 
)
static

Converts a color. Channels that can't be converted are initialized to 0. When converting to and from float colors, normalized color values are used. Float values that do not fall within the 0-1 range are clamped. When the target does not manage it's own color values, ie: holds pointers to color values in memory, the values that are pointed to are overridden by the result of the conversion. This makes it possible (for example) to write colors directly in to a bitmap without having to copy them over.

Parameters
sourcethe color to convert
targetholds the converted color values

◆ convertColor() [2/2]

static void convertColor ( const BaseColor source,
BaseColor target,
const Converter converter 
)
static

Converts the color values in source Color to target Color using the given conversion method It's required that the from color has an equal or higher amount of color channels Therefore this conversion is valid: RGBA8 to RGBFloat, but not: RGB8 to RGBAFloat The following is also valid: RGBColorData16 to RGBColorFloat or, RGBAColorData8 to RGBColorData16 When converting to and from float colors, normalized color values are used. Float values that do not fall within the 0-1 range are clamped When the target does not manage it's own color values, ie: holds pointers to color values in memory, the values that are pointed to are overridden by the result of the conversion This makes it possible (for example) to write colors directly in to a bitmap without having to copy them over.

This call is faster than the default convertColor method and is recommended to be used in loops! Make sure to call getConverter() before staring the loop to acquire the right conversion method

Parameters
sourcethe color to convert
targetholds the converted color values
converterthe method used to convert source color into target color

◆ getConverter() [1/2]

static Converter getConverter ( const BaseColor source,
const BaseColor target 
)
static
Returns
a color converter to convert source color in to target color, nullptr if no such converter exists Use this call when dealing with the same color conversion multiple times in, for example, a loop

◆ getConverter() [2/2]

Converter getConverter ( const BaseColor target) const
Returns
a color converter to convert source color in to target color, nullptr if no such converter exists Use this call when dealing with the same color conversion multiple times in, for example, a loop.

◆ getData() [1/2]

virtual const void* getData ( int  channel) const
pure virtual
Parameters
channelnumber of the channel to get the data for
Returns
the data associated with the channel at the index

◆ getData() [2/2]

virtual void* getData ( int  channel)
pure virtual
Parameters
channelnumber of the channel to get the data for
Returns
the data associated with the channel at the index

◆ getNumberOfChannels()

int getNumberOfChannels ( ) const
Returns
the number of channels associated with this color

◆ getValueType()

virtual rtti::TypeInfo getValueType ( ) const
pure virtual
Returns
the color value data type

Implemented in Color< T, CHANNELS >.

◆ isPointer()

virtual bool isPointer ( ) const
pure virtual
Returns
if the data contained by this color points to values in memory

Implemented in Color< T, CHANNELS >.

◆ operator!=()

bool operator!= ( const BaseColor rhs)
delete

◆ operator==()

bool operator== ( const BaseColor rhs)
delete

◆ size()

int size ( ) const
Returns
the total size in bytes of the color

◆ valueSize()

int valueSize ( ) const
Returns
the size of a single channel in bytes
nap::BaseColor::convert
void convert(BaseColor &target) const
nap::RGBColor8
RGBColor< uint8 > RGBColor8
Definition: color.h:575
nap::RGBAColorFloat
RGBAColor< float > RGBAColorFloat
Definition: color.h:580