Skip to content

_olc::Pixel

Javidx9 edited this page Dec 15, 2020 · 3 revisions

Description

A struct that represents a single pixel in "engine" space. Pixels have a width and a height value and are rendered in "screen space". Therefore, the minimum useful size of a pixel is 1x1, which then maps directly onto display pixels on-screen.

Pixels consist of 4 components, red, green, blue and alpha. The configuration of a pixel can be set in various ways. By default Pixel alpha values are set to 255 (no transparency).

// Set explicitly
olc::Pixel p;
p.r = 100;
p.g = 200;
p.b = 150;
p.a = 255;

// Set via constructor (alpha is default)
olc::Pixel p(100,200,150);

// Set via constructor with alpha
olc::Pixel p(100,200,150,255);

// Set via HEX
olc::Pixel p(0xFF96C864);
p.n = 0xFF96C864 // Note order is ABGR

// Set Via Float
olc::Pixel p = olc::PixelF(0.1f, 0.2f, 0.3f, 1.0f);

A variety of convenience constants have been created:

  • olc::WHITE (255, 255, 255)
  • olc::GREY (192, 192, 192)
  • olc::DARK_GREY (128, 128, 128)
  • olc::VERY_DARK_GREY (64, 64, 64)
  • olc::RED (255, 0, 0)
  • olc::DARK_RED (128, 0, 0)
  • olc::VERY_DARK_RED (64, 0, 0)
  • olc::YELLOW (255, 255, 0)
  • olc::DARK_YELLOW (128, 128, 0)
  • olc::VERY_DARK_YELLOW (64, 64, 0)
  • olc::GREEN (0, 255, 0)
  • olc::DARK_GREEN (0, 128, 0)
  • olc::VERY_DARK_GREEN (0, 64, 0)
  • olc::CYAN (0, 255, 255)
  • olc::DARK_CYAN (0, 128, 128)
  • olc::VERY_DARK_CYAN (0, 64, 64)
  • olc::BLUE (0, 0, 255)
  • olc::DARK_BLUE (0, 0, 128)
  • olc::VERY_DARK_BLUE (0, 0, 64)
  • olc::MAGENTA (255, 0, 255)
  • olc::DARK_MAGENTA (128, 0, 128)
  • olc::VERY_DARK_MAGENTA (64, 0, 64)
  • olc::BLACK (0, 0, 0)
  • olc::BLANK (0, 0, 0, 0)

These can be used in place of a olc::Pixel argument, for example:

Draw(10, 10, olc::DARK_BLUE); // Draws a dark blue pixel at location (10,10)

Fields

uint8_t r

The red component of the pixel's colour, ranging from 0 to 255.

uint8_t g

The green component of the pixel's colour, ranging from 0 to 255.

uint8_t b

The blue component of the pixel's colour, ranging from 0 to 255.

uint8_t a

The alpha component of the pixel's colour, ranging from 0 to 255. Alpha is used to implement transparency, where 0 is 100% transparent, and 255 is 100% opaque.

uint32_t n

A union of the 4 colour components represented as a 32-bit word in the format ABGR.


Methods

olc::Pixel::Pixel()

Constructs a black, opaque pixel.


olc::Pixel::Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 0xFF)

Constructs a custom pixel, with optional alpha component. The alpha component will default to opaque.


olc::Pixel::Pixel(uint32_t p)

Constructs a custom pixel from a 32-bit word in the format 0xAABBGGRR.


bool olc::Pixel::operator ==(const Pixel& p) const

Compares if two pixels are equal in colour.


bool olc::Pixel::operator !=(const Pixel& p) const

Compares if two pixels are not equal in colour.


olc::Pixel olc::Pixel::operator * (const float i) const

Scales a pixel via multiplication by factor i, producing a new pixel. Colour components will be clamped between 0 and 255. For example, multiplying by 1.5 will typically lighten the pixel, whereas multiplying by 0.5 will darken the pixel. Alpha component is not affected.


olc::Pixel olc::Pixel::operator / (const float i) const

Scales a pixel via division by factor i, producing a new pixel. Colour components will be clamped between 0 and 255. For example, dividing by 1.5 will typically darken the pixel, whereas dividing by 0.5 will lighten the pixel. Alpha component is not affected.


olc::Pixel& olc::Pixel::operator *=(const float i)

Scales the pixel via multiplication by factor i. Colour components will be clamped between 0 and 255. For example, multiplying by 1.5 will typically lighten the pixel, whereas multiplying by 0.5 will darken the pixel. Alpha component is not affected.


olc::Pixel& olc::Pixel::operator /=(const float i)

Scales the pixel via division by factor i. Colour components will be clamped between 0 and 255. For example, dividing by 1.5 will typically darken the pixel, whereas dividing by 0.5 will lighten the pixel. Alpha component is not affected.


olc::Pixel olc::Pixel::operator + (const Pixel& p) const

Adds two pixels, returning a new pixel. Colour components will be clamped between 0 and 255. Alpha component is not affected.


olc::Pixel olc::Pixel::operator - (const Pixel& p) const

Subtracts two pixels, returning a new pixel. Colour components will be clamped between 0 and 255. Alpha component is not affected.


olc::Pixel& olc::Pixel::operator +=(const Pixel& p)

Adds a pixel to this pixel. Colour components will be clamped between 0 and 255. Alpha component is not affected.


olc::Pixel& olc::Pixel::operator -=(const Pixel& p)

Subtracts a pixel from this pixel. Colour components will be clamped between 0 and 255. Alpha component is not affected.


olc::Pixel olc::Pixel::inv() const

Inverts a pixel, producing a new pixel. Each colour component will be subtracted from 255 producing the "negative" colour. Alpha component is not affected.


Utility Functions

olc::Pixel olc::PixelF(float red, float green, float blue, float alpha = 1.0f)

Constructs a regular pixel by specifying normalised components, where 0 is fully transparent and 1 is fully opaque.


olc::Pixel olc::PixelLerp(const olc::Pixel& p1, const olc::Pixel& p2, float t)

Constructs a pixel that is in between two other pixels in RGB space, so if p1 were black, and p2 were white, if t = 0, the returned pixel would be black; if t = 0.5, it would be grey; if t = 1.0 it would be white.

Example

for(int x = 0; x < 10; x++)
{
    olc::Pixel p = olc::PixelLerp(olc::BLACK, olc::WHITE, float(x) / 10.0f);
    Draw(x, 10, p);
}


Clone this wiki locally