Skip to main content

IGraphics Structs

IGraphics uses several key data structures to manage colors, rectangles, text, blending, and other UI-related state. These structs are designed to be platform-independent and work consistently across all drawing backends.

IRECT

Rectangular area representation. Always specified in 1:1 pixels with (0,0) at top-left.

Constructor

IRECT(float l, float t, float r, float b)
l
float
required
Left edge
t
float
required
Top edge
r
float
required
Right edge
b
float
required
Bottom edge
IRECT rect(10, 20, 110, 120); // x=10, y=20, width=100, height=100

Static Constructors

MakeXYWH

static IRECT MakeXYWH(float l, float t, float w, float h)
Create an IRECT from position and size.
IRECT rect = IRECT::MakeXYWH(10, 20, 100, 50);

MakeMidXYWH

static IRECT MakeMidXYWH(float x, float y, float w, float h)
Create an IRECT centered at (x, y) with given size.

Properties

Dimensions

float W() const  // Width
float H() const  // Height
float MW() const // Middle width (center X)
float MH() const // Middle height (center Y)
float Area() const
IRECT rect(0, 0, 100, 50);
float centerX = rect.MW(); // 50
float centerY = rect.MH(); // 25

Set Operations

Union

IRECT Union(const IRECT& rhs) const
Create a rectangle containing both rectangles.

Intersect

IRECT Intersect(const IRECT& rhs) const
Create a rectangle of the overlapping area.

Intersects

bool Intersects(const IRECT& rhs) const
Check if rectangles share any pixels.

Contains

bool Contains(const IRECT& rhs) const
bool Contains(float x, float y) const
Check if a rectangle or point is fully contained.

Subdivision

GetGridCell

IRECT GetGridCell(int row, int col, int nRows, int nColumns) const
IRECT GetGridCell(int cellIndex, int nRows, int nColumns, 
                 EDirection dir = EDirection::Horizontal, int nCells = 1) const
Divide into a grid and get a specific cell.
IRECT bounds = g.GetBounds();
IRECT topLeft = bounds.GetGridCell(0, 0, 2, 2);    // Top-left quarter
IRECT topRight = bounds.GetGridCell(0, 1, 2, 2);   // Top-right quarter
IRECT cell5 = bounds.GetGridCell(5, 3, 4);         // Cell index 5 in 3x4 grid

SubRect

IRECT SubRectVertical(int numSlices, int sliceIdx) const
IRECT SubRectHorizontal(int numSlices, int sliceIdx) const
Divide into equal slices and get one.
IRECT column = bounds.SubRectHorizontal(3, 1); // Middle third horizontally

FracRect

IRECT FracRectHorizontal(float frac, bool rhs = false) const
IRECT FracRectVertical(float frac, bool fromTop = false) const
Get a fractional portion of the rectangle.
IRECT leftHalf = bounds.FracRectHorizontal(0.5f);      // Left 50%
IRECT rightThird = bounds.FracRectHorizontal(0.33f, true); // Right 33%

Edge Operations

Get From Edge

IRECT GetFromLeft(float amount) const
IRECT GetFromRight(float amount) const
IRECT GetFromTop(float amount) const
IRECT GetFromBottom(float amount) const
Get a subrect from an edge.
IRECT header = bounds.GetFromTop(50);  // Top 50 pixels
IRECT sidebar = bounds.GetFromLeft(200); // Left 200 pixels

Reduce From Edge

IRECT GetReducedFromLeft(float amount) const
IRECT GetReducedFromRight(float amount) const
IRECT GetReducedFromTop(float amount) const
IRECT GetReducedFromBottom(float amount) const
Remove an amount from an edge.

Mutating Edge Operations

IRECT ReduceFromLeft(float amount)   // Returns removed part
IRECT ReduceFromRight(float amount)
IRECT ReduceFromTop(float amount)
IRECT ReduceFromBottom(float amount)
Reduce and return the removed portion.
IRECT bounds = g.GetBounds();
IRECT header = bounds.ReduceFromTop(50);  // Extract top 50px
IRECT footer = bounds.ReduceFromBottom(30); // Extract bottom 30px
// bounds now contains the middle section

Padding

Pad

void Pad(float padding)
void Pad(float padL, float padT, float padR, float padB)
void HPad(float padding)  // Horizontal only
void VPad(float padding)  // Vertical only
Expand (positive) or contract (negative) the rectangle.

GetPadded

IRECT GetPadded(float padding) const
IRECT GetPadded(float padL, float padT, float padR, float padB) const
IRECT GetHPadded(float padding) const
IRECT GetVPadded(float padding) const
Get a padded copy.
IRECT inner = bounds.GetPadded(-10);  // Shrink by 10px on all sides
IRECT outer = bounds.GetPadded(5);    // Expand by 5px on all sides

Centering

GetCentredInside

IRECT GetCentredInside(float w, float h = 0.f) const
IRECT GetCentredInside(const IRECT& sr) const
IRECT GetCentredInside(const IBitmap& bitmap) const
Get a rectangle centered within this one.
IRECT centered = bounds.GetCentredInside(100, 100); // 100x100 square in center
IRECT bitmapRect = bounds.GetCentredInside(myBitmap);

Transformation

Translate

void Translate(float x, float y)
IRECT GetTranslated(float x, float y) const
IRECT GetHShifted(float x) const
IRECT GetVShifted(float y) const
Move the rectangle.

Scale

void Scale(float scale)
void ScaleAboutCentre(float scale)
IRECT GetScaled(float scale) const
IRECT GetScaledAboutCentre(float scale) const
Scale the rectangle.
IRECT scaled = bounds.GetScaledAboutCentre(0.8f); // 80% size, centered

Pixel Alignment

PixelAlign

void PixelAlign()
void PixelAlign(float scale)
IRECT GetPixelAligned() const
IRECT GetPixelAligned(float scale) const
Align to pixel boundaries (expand outward).

PixelSnap

void PixelSnap()
void PixelSnap(float scale)
IRECT GetPixelSnapped() const
IRECT GetPixelSnapped(float scale) const
Snap to nearest pixels (may shrink).
PixelAlign expands to cover pixels, PixelSnap rounds to nearest. Use PixelAlign for drawing bounds to avoid gaps.

Alignment

void HAlignTo(const IRECT& sr, EAlign align)
void VAlignTo(const IRECT& sr, EVAlign align)
IRECT GetHAlignedTo(const IRECT& sr, EAlign align) const
IRECT GetVAlignedTo(const IRECT& sr, EVAlign align) const
Align to another rectangle.
IRECT button = IRECT::MakeXYWH(0, 0, 100, 40);
button.HAlignTo(bounds, EAlign::Center);
button.VAlignTo(bounds, EVAlign::Bottom);

Interpolation

static IRECT LinearInterpolateBetween(const IRECT& start, const IRECT& dest, float progress)
Linear interpolation between two rectangles.

IColor

RGB color with alpha channel (ARGB format, 0-255 per channel).

Constructor

IColor(int a = 255, int r = 0, int g = 0, int b = 0)
a
int
default:"255"
Alpha (0=transparent, 255=opaque)
r
int
default:"0"
Red (0-255)
g
int
default:"0"
Green (0-255)
b
int
default:"0"
Blue (0-255)
IColor red(255, 255, 0, 0);
IColor semiTransparentBlue(128, 0, 0, 255);

Static Constructors

FromColorCode

static IColor FromColorCode(int colorCode, int A = 0xFF)
Create from hex color code.
IColor blue = IColor::FromColorCode(0x0000FF);
IColor red = IColor::FromColorCode(0xFF0000, 128); // Semi-transparent

FromColorCodeStr

static IColor FromColorCodeStr(const char* hexStr)
Create from hex string.
IColor color = IColor::FromColorCodeStr("#3498db");

FromHSLA

static IColor FromHSLA(float h, float s, float l, float a = 1.f)
Create from HSL values (0-1 range).
h
float
required
Hue (0-1)
s
float
required
Saturation (0-1)
l
float
required
Lightness (0-1)

Methods

SetOpacity

void SetOpacity(float alpha)
IColor WithOpacity(float alpha) const
Set or get color with different opacity (0-1).
IColor faded = COLOR_RED.WithOpacity(0.5f); // 50% transparent red

Contrast

void Contrast(float c)
IColor WithContrast(float c) const
Adjust color contrast (-1 to +1).
IColor lighter = baseColor.WithContrast(0.3f);
IColor darker = baseColor.WithContrast(-0.3f);

Color Space Conversion

void GetHSLA(float& h, float& s, float& l, float& a) const
void GetRGBf(float* rgbf) const    // 3-element float array
void GetRGBAf(float* rgbaf) const  // 4-element float array

GetLuminosity

int GetLuminosity() const
Get perceived brightness (0-255).

Randomize

void Randomise(int alpha = 255)
static IColor GetRandomColor(bool randomAlpha = false)

Interpolation

static IColor LinearInterpolateBetween(const IColor& start, const IColor& dest, float progress)
IColor gradient = IColor::LinearInterpolateBetween(COLOR_BLUE, COLOR_RED, 0.5f);

Predefined Colors

const IColor COLOR_TRANSPARENT;
const IColor COLOR_BLACK;
const IColor COLOR_WHITE;
const IColor COLOR_GRAY;
const IColor COLOR_LIGHT_GRAY;
const IColor COLOR_MID_GRAY;
const IColor COLOR_DARK_GRAY;
const IColor COLOR_RED;
const IColor COLOR_GREEN;
const IColor COLOR_BLUE;
const IColor COLOR_YELLOW;
const IColor COLOR_ORANGE;

IText

Text formatting and style information.

Constructor

IText(float size = DEFAULT_TEXT_SIZE,
     const IColor& color = DEFAULT_TEXT_FGCOLOR,
     const char* fontID = nullptr,
     EAlign align = EAlign::Center,
     EVAlign valign = EVAlign::Middle,
     float angle = 0,
     const IColor& TEBGColor = DEFAULT_TEXTENTRY_BGCOLOR,
     const IColor& TEFGColor = DEFAULT_TEXTENTRY_FGCOLOR)
size
float
default:"DEFAULT_TEXT_SIZE"
Font size in pixels
color
const IColor&
default:"DEFAULT_TEXT_FGCOLOR"
Text color
fontID
const char*
default:"nullptr"
Font identifier (loaded via LoadFont)
align
EAlign
default:"EAlign::Center"
Horizontal alignment (Near/Center/Far)
valign
EVAlign
default:"EVAlign::Middle"
Vertical alignment (Top/Middle/Bottom)
angle
float
default:"0"
Rotation angle in degrees clockwise
IText titleText(24, COLOR_BLACK, "Roboto-Bold", EAlign::Center, EVAlign::Top);
IText bodyText(14, COLOR_DARK_GRAY, "Roboto-Regular");

Builder Methods

IText WithFGColor(const IColor& fgColor) const
IText WithAlign(EAlign align) const
IText WithVAlign(EVAlign valign) const
IText WithSize(float size) const
IText WithAngle(float angle) const
IText WithFont(const char* fontID) const
Chainable methods for creating variations.
IText base(16, COLOR_BLACK, "Roboto-Regular");
IText larger = base.WithSize(20);
IText centered = base.WithAlign(EAlign::Center);
IText rotated = base.WithAngle(45.f);

IBitmap

Bitmap image abstraction (doesn’t own pixel data).

Methods

int W() const              // Overall width
int H() const              // Overall height
int FW() const             // Single frame width
int FH() const             // Single frame height
int N() const              // Number of frames
float GetScale() const     // Bitmap scale
float GetDrawScale() const
bool IsValid() const
const WDL_String& GetResourceName() const
IBitmap knobBitmap = g.LoadBitmap("knob.png", 60, true); // 60 frames horizontal
int frameWidth = knobBitmap.FW();
int frameHeight = knobBitmap.FH();

ISVG

SVG image abstraction.

Methods

float W() const
float H() const
bool IsValid() const
ISVG logo = g.LoadSVG("logo.svg");
g.DrawSVG(logo, bounds);

IBlend

Blending/compositing mode.

Constructor

IBlend(EBlend type = EBlend::Default, float weight = 1.0f)
type
EBlend
default:"EBlend::Default"
Blend type
weight
float
default:"1.0"
Blend weight/opacity (0-1)

Predefined Blends

const IBlend BLEND_75;  // 75% opacity
const IBlend BLEND_50;  // 50% opacity
const IBlend BLEND_25;  // 25% opacity
const IBlend BLEND_10;
const IBlend BLEND_05;
g.FillRect(COLOR_RED, bounds, &BLEND_50); // 50% transparent red

IMouseMod

Mouse modifier state.

Constructor

IMouseMod(bool l = false, bool r = false, bool s = false, bool c = false, bool a = false)

Fields

L
bool
Left mouse button
R
bool
Right mouse button
S
bool
Shift key
C
bool
Control/Command key
A
bool
Alt/Option key
void OnMouseDown(float x, float y, const IMouseMod& mod) {
  if (mod.R) {
    // Right click
  }
  if (mod.S && mod.L) {
    // Shift + left click
  }
  if (mod.C) {
    // Ctrl/Cmd modifier
  }
}

IStrokeOptions

Path stroke configuration.

Fields

float mMiterLimit = 10.f;
bool mPreserve = false;
ELineCap mCapOption = ELineCap::Butt;
ELineJoin mJoinOption = ELineJoin::Miter;
DashOptions mDash;

DashOptions

DashOptions(float* array, float offset, int count)
Define dash pattern (max 8 floats).
float dashPattern[] = {5.f, 3.f}; // 5px dash, 3px gap
IStrokeOptions::DashOptions dash(dashPattern, 0.f, 2);
IStrokeOptions opts;
opts.mDash = dash;
g.PathStroke(IPattern(COLOR_BLACK), 2.f, opts);

IFillOptions

Path fill configuration.

Constructor

IFillOptions(bool preserve = false, EFillRule fillRule = EFillRule::Winding)
preserve
bool
default:"false"
Preserve path after filling
fillRule
EFillRule
default:"EFillRule::Winding"
Fill rule (Winding or EvenOdd)

IRECTList

Manage and optimize lists of rectangles.

Methods

void Add(const IRECT& rect)
void Set(int idx, const IRECT& rect)
const IRECT& Get(int idx) const
int Size() const
void Clear()
IRECT Bounds()  // Union of all rects
void Optimize() // Merge and remove overlaps
void PixelAlign()
int Find(float x, float y) const
IRECTList dirtyRects;
dirtyRects.Add(control1->GetRECT());
dirtyRects.Add(control2->GetRECT());
dirtyRects.Optimize(); // Merge overlapping areas

GetFracGrid

static bool GetFracGrid(const IRECT& input, IRECTList& rects,
                       const std::initializer_list<float>& rowFractions,
                       const std::initializer_list<float>& colFractions,
                       EDirection layoutDir = EDirection::Horizontal)
Divide a rectangle into a proportional grid.
IRECTList cells;
IRECTList::GetFracGrid(bounds, cells, 
  {0.3f, 0.7f},  // 30% top row, 70% bottom row
  {0.5f, 0.5f}   // 50% each column
);
// Creates 4 cells: top-left (30%x50%), top-right (30%x50%), 
//                  bottom-left (70%x50%), bottom-right (70%x50%)

IMatrix

Affine transformation matrix for path operations.

Methods

IMatrix& Translate(float x, float y)
IMatrix& Scale(float x, float y)
IMatrix& Rotate(float angle)  // Degrees
IMatrix& Skew(float xa, float ya)
IMatrix& Transform(float x, float y, float& xOut, float& yOut)

Common Patterns

IRECT CreateLayout(IGraphics& g) {
  IRECT bounds = g.GetBounds().GetPadded(-20);
  
  IRECT header = bounds.ReduceFromTop(50);
  IRECT footer = bounds.ReduceFromBottom(30);
  
  IRECT leftPanel = bounds.FracRectHorizontal(0.3f);
  IRECT rightPanel = bounds.FracRectHorizontal(0.7f, true);
  
  return rightPanel;
}

See Also

IGraphics

Main graphics context

IControl

Control base class