KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | List of all members
kjb::PixelRGBA Struct Reference

Wrapped version of the C struct Pixel, with Alpha (opacity). More...

#include <i_pixel.h>

Inheritance diagram for kjb::PixelRGBA:

Public Member Functions

 PixelRGBA ()
 Default ctor, which leaves all fields uninitialized. More...
 
 PixelRGBA (const kjb_c::Pixel &p)
 This builds a valid pixel from another. More...
 
 PixelRGBA (float rr, float gg, float bb, float aa=255.0f)
 This builds a valid pixel with given values for color channels. More...
 
PixelRGBAow_clamp ()
 Clamp all channels of the pixel to the range 0..255 inclusive. More...
 
PixelRGBAow_clamp (float minval, float maxval)
 Clamp all channels to a specified range, overwriting. More...
 
PixelRGBA clamp () const
 Return a new pixel with all channels clamped to range 0..255. More...
 
PixelRGBA clamp (float minval, float maxval) const
 Return a new pixel with all channels clamped; non-overwriting. More...
 
PixelRGBAow_floor ()
 Truncate pixel channel values to integers; overwriting. More...
 
PixelRGBA floor () const
 Return new pixel with integer channel values. More...
 
PixelRGBAoperator+= (const kjb_c::Pixel &p)
 Adds another pixel (channel-wise) to this pixel, overwriting. More...
 
PixelRGBA operator+ (const kjb_c::Pixel &p) const
 This will add corresponding channels, creating a new pixel. More...
 
PixelRGBAoperator*= (const PixelRGBA &p)
 Overwrite a pixel by multiplying it by p. More...
 
PixelRGBAoperator*= (float k)
 Overwrite a pixel by scaling its value (r, g, b, and alpha too) More...
 
bool operator== (const kjb_c::Pixel &p) const
 Test for EXACT pixel equality in all channels. More...
 
bool operator!= (const kjb_c::Pixel &p) const
 Test for ANY pixel inequality in any channel. More...
 
std::string as_hex_triplet () const
 express RGB values in an HTML-style string More...
 

Static Public Member Functions

static PixelRGBA create_gray (float f)
 A "named constructor" to build a single opaque grayscale pixel. More...
 
static void ow_clamp_channel (float *chan)
 Clamp one channel of the Pixel to the range 0..255 inclusive. More...
 
static void ow_clamp_channel (float *chan, float minval, float maxval)
 Clamp one channel to a specified range. More...
 

Detailed Description

Wrapped version of the C struct Pixel, with Alpha (opacity).

This is a struct since there is (currently) no need to make such a simple item have anything private. The advantage of wrapping this at all is that there are commonly-performed operations that are naturally expressed as methods. Namely:

Example: given pixels p, q this yields a Pixel: 0.5*(p.clamp()+q)

This struct assumes the user is not interested in the "Invalid Pixel" structure, and is perhaps (but not necessarily) using alpha (opacity). That's why the name has the RGBA suffix.

Remember that if you add pixels or scale pixels, the alpha channels also add and scale. So for the common case (opaque pixels, and 8-bit channels), the alpha channel is already at 255.0, so you might need to clamp() or to ow_clamp() after an add or scale operation, otherwise alpha could go overrange.

[CODE POLICE] TODO: Rename this class to follow kjb standard for type names (No camelcase Only first letter may be capitalized): Pixel_rgba

Constructor & Destructor Documentation

kjb::PixelRGBA::PixelRGBA ( )
inline

Default ctor, which leaves all fields uninitialized.

Warning
"Uninitialized" often means "bug-causing" and "troublesome."
kjb::PixelRGBA::PixelRGBA ( const kjb_c::Pixel &  p)
inline

This builds a valid pixel from another.

Build a pixel from another. It receives a kjb_c::Pixel so as to work with PixelRGBA as well.

kjb::PixelRGBA::PixelRGBA ( float  rr,
float  gg,
float  bb,
float  aa = 255.0f 
)
inline

This builds a valid pixel with given values for color channels.

Note that despite our massive dynamic range, the default value of aa (the alpha channel) is a mere 255.0f. This is the most common use-case, that of 8 bits of alpha, maximum opacity. However, it is ugly. For high dynamic range images, this is probably not your preferred value for the default opacity.

Member Function Documentation

std::string kjb::PixelRGBA::as_hex_triplet ( ) const
inline

express RGB values in an HTML-style string

PixelRGBA kjb::PixelRGBA::clamp ( ) const
inline

Return a new pixel with all channels clamped to range 0..255.

Observe that this routine does not, however, truncate pixels to integer values. If you need that, use the floor() method.

PixelRGBA kjb::PixelRGBA::clamp ( float  minval,
float  maxval 
) const
inline

Return a new pixel with all channels clamped; non-overwriting.

static PixelRGBA kjb::PixelRGBA::create_gray ( float  f)
inlinestatic

A "named constructor" to build a single opaque grayscale pixel.

PixelRGBA kjb::PixelRGBA::floor ( ) const
inline

Return new pixel with integer channel values.

bool kjb::PixelRGBA::operator!= ( const kjb_c::Pixel &  p) const
inline

Test for ANY pixel inequality in any channel.

Warning
Pixel stores channel values as floating point numbers! So instead of p != q you might want p.floor() != q.floor()
PixelRGBA& kjb::PixelRGBA::operator*= ( const PixelRGBA p)
inline

Overwrite a pixel by multiplying it by p.

This performs a simple channel-wise product.

Warning
Don't forget to ow_clamp() if you think you might need to.
PixelRGBA& kjb::PixelRGBA::operator*= ( float  k)
inline

Overwrite a pixel by scaling its value (r, g, b, and alpha too)

Warning
Don't forget to ow_clamp() if you think you might need to.
PixelRGBA kjb::PixelRGBA::operator+ ( const kjb_c::Pixel &  p) const
inline

This will add corresponding channels, creating a new pixel.

Warning
Don't forget to clamp() if you think you might need to.
PixelRGBA& kjb::PixelRGBA::operator+= ( const kjb_c::Pixel &  p)
inline

Adds another pixel (channel-wise) to this pixel, overwriting.

Warning
Don't forget to ow_clamp() if you think you might need to.
bool kjb::PixelRGBA::operator== ( const kjb_c::Pixel &  p) const
inline

Test for EXACT pixel equality in all channels.

Warning
Pixel stores channel values as floating point numbers! So instead of p==q you might want p.floor()==q.floor()
PixelRGBA& kjb::PixelRGBA::ow_clamp ( )
inline

Clamp all channels of the pixel to the range 0..255 inclusive.

Returns
Lvalue of the modified PixelRGBA

Observe that this routine does not, however, truncate pixels to integer values. If you need that, use the ow_floor() or floor() method. This method has overwriting semantics.

PixelRGBA& kjb::PixelRGBA::ow_clamp ( float  minval,
float  maxval 
)
inline

Clamp all channels to a specified range, overwriting.

Returns
Lvalue of the modified PixelRGBA
static void kjb::PixelRGBA::ow_clamp_channel ( float *  chan)
inlinestatic

Clamp one channel of the Pixel to the range 0..255 inclusive.

Observe that this routine does not, however, truncate pixels to integer values. If you need that, use the ow_floor() or floor() method. The 0..255 range is hardcoded in since it is such a common use case.

static void kjb::PixelRGBA::ow_clamp_channel ( float *  chan,
float  minval,
float  maxval 
)
inlinestatic

Clamp one channel to a specified range.

PixelRGBA& kjb::PixelRGBA::ow_floor ( )
inline

Truncate pixel channel values to integers; overwriting.

Returns
Lvalue of the modified PixelRGBA

The documentation for this struct was generated from the following file: