Luna/libui/src/Canvas.cpp

44 lines
1.1 KiB
C++

#include <string.h>
#include <ui/Canvas.h>
namespace ui
{
Canvas Canvas::create(u8* ptr, int width, int height)
{
return Canvas { .width = width, .height = height, .stride = width, .ptr = ptr };
}
Result<Canvas> Canvas::subcanvas(Point begin, int w, int h, bool clamp)
{
if (!clamp)
{
if (begin.x + w > width) return err(ERANGE);
if (begin.y + h > height) return err(ERANGE);
}
else
{
if (begin.x + w > width) w = width - begin.x;
if (begin.y + h > height) h = height - begin.y;
}
u8* p = ptr + begin.x * sizeof(Color) + (begin.y * sizeof(Color) * stride);
return Canvas { .width = w, .height = h, .stride = stride, .ptr = p };
}
void Canvas::fill(Color color)
{
u8* p = ptr;
for (int i = 0; i < height; i++)
{
u32* colorp = (u32*)p;
for (int j = 0; j < width; j++)
{
*colorp = color.raw;
colorp++;
}
p += stride * sizeof(Color);
}
}
}