pixel

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: BSD-3-Clause Imports: 3 Imported by: 21

Documentation

Overview

Package pixel contains pixel format definitions used in various displays and fast operations on them.

This package is just a base for pixel operations, it is _not_ a graphics library. It doesn't define circles, lines, etc - just the bare minimum graphics operations needed plus the ones that need to be specialized per pixel format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewColor

func NewColor[T Color](r, g, b uint8) T

NewColor returns the given color based on the RGB values passed in the parameters. The input value is assumed to be in sRGB color space.

func NewLinearColor

func NewLinearColor[T Color](r, g, b uint8) T

NewLinearColor returns the given color based on the linear RGB values passed in the parameters. Use this if the RGB values are actually linear colors (like those that are used in most RGB LEDs) and not when it is in the usual sRGB color space (which is not linear).

The input is assumed to be in the linear sRGB color space.

Types

type BaseColor

type BaseColor interface {
	// The number of bits when stored.
	// This means for example that RGB555 (which is still stored as a 16-bit
	// integer) returns 16, while RGB444 returns 12.
	BitsPerPixel() int

	// Return the given color in color.RGBA format, which is always sRGB. The
	// alpha channel is always 255.
	RGBA() color.RGBA
}

BaseColor contains all the methods needed in a color format. This can be used in display drivers that want to define their own Color type with just the pixel formats the display supports.

type Color

type Color interface {
	RGB888 | RGB565BE | RGB555 | RGB444BE

	BaseColor
}

Pixel with a particular color, matching the underlying hardware of a particular display. Each pixel is at least 1 byte in size. The color format is sRGB (or close to it) in all cases.

type Image

type Image[T Color] struct {
	// contains filtered or unexported fields
}

Image buffer, used for working with the native image format of various displays. It works a lot like a slice: it can be rescaled while reusing the underlying buffer and should be passed around by value.

func NewImage

func NewImage[T Color](width, height int) Image[T]

NewImage creates a new image of the given size.

func (Image[T]) FillSolidColor

func (img Image[T]) FillSolidColor(color T)

FillSolidColor fills the entire image with the given color. This may be faster than setting individual pixels.

func (Image[T]) Get

func (img Image[T]) Get(x, y int) T

Get returns the color at the given index.

func (Image[T]) Len

func (img Image[T]) Len() int

Len returns the number of pixels in this image buffer.

func (Image[T]) LimitHeight

func (img Image[T]) LimitHeight(height int) Image[T]

LimitHeight returns a subimage with the bottom part cut off, as specified by height.

func (Image[T]) RawBuffer

func (img Image[T]) RawBuffer() []uint8

RawBuffer returns a byte slice that can be written directly to the screen using DrawRGBBitmap8.

func (Image[T]) Rescale

func (img Image[T]) Rescale(width, height int) Image[T]

Rescale returns a new Image buffer based on the img buffer. The contents is undefined after the Rescale operation, and any modification to the returned image will overwrite the underlying image buffer in undefined ways. It will panic if width*height is larger than img.Len().

func (Image[T]) Set

func (img Image[T]) Set(x, y int, c T)

Set sets the pixel at x, y to the given color. Use FillSolidColor to efficiently fill the entire image buffer.

func (Image[T]) Size

func (img Image[T]) Size() (int, int)

Size returns the image size.

type RGB444BE

type RGB444BE uint16

Color format that is supported by the ST7789 for example. It may be a bit faster to use than RGB565BE on very slow SPI buses.

The color format is native endian as a uint16 (0000rrrr_ggggbbbb), not big endian which you might expect. I tried swapping the bytes, but it didn't have much of a performance impact and made the code harder to read. It is stored as a 12-bit big endian value in Image[RGB444BE] though.

func NewRGB444BE

func NewRGB444BE(r, g, b uint8) RGB444BE

func (RGB444BE) BitsPerPixel

func (c RGB444BE) BitsPerPixel() int

func (RGB444BE) RGBA

func (c RGB444BE) RGBA() color.RGBA

type RGB555

type RGB555 uint16

Color format used on the GameBoy Advance among others.

Colors are stored as native endian values, with bits 0bbbbbgg_gggrrrrr (red is least significant, blue is most significant).

func NewRGB555

func NewRGB555(r, g, b uint8) RGB555

func (RGB555) BitsPerPixel

func (c RGB555) BitsPerPixel() int

func (RGB555) RGBA

func (c RGB555) RGBA() color.RGBA

type RGB565BE

type RGB565BE uint16

RGB565 as used in many SPI displays. Stored as a big endian value.

The color format in integer form is gggbbbbb_rrrrrggg on little endian systems, which is the standard RGB565 format but with the top and bottom bytes swapped.

There are a few alternatives to this weird big-endian format, but they're not great:

  • Storing the value in two 8-bit stores (to make the code endian-agnostic) incurs too much of a performance penalty.
  • Swapping the upper and lower bits just before storing. This is still less efficient than it could be, since colors are usually constructed once and then reused in many store operations. Doing the swap once instead of many times for each store is a performance win.

func NewRGB565BE

func NewRGB565BE(r, g, b uint8) RGB565BE

func (RGB565BE) BitsPerPixel

func (c RGB565BE) BitsPerPixel() int

func (RGB565BE) RGBA

func (c RGB565BE) RGBA() color.RGBA

type RGB888

type RGB888 struct {
	R, G, B uint8
}

RGB888 format, more commonly used in other places (desktop PC displays, CSS, etc). Less commonly used on embedded displays due to the higher memory usage.

func NewRGB888

func NewRGB888(r, g, b uint8) RGB888

func (RGB888) BitsPerPixel

func (c RGB888) BitsPerPixel() int

func (RGB888) RGBA

func (c RGB888) RGBA() color.RGBA

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL