# Graphics

The Storm standard library provides basic primitives for storing graphics in order to facilitate interoperability between libraries, particularly libraries written in C++. As such, the facilities provided by the standard library are quite sparse. However, the graphics library provides facilities for loading images from streams, and the UI library allows drawing them to the screen.

The graphics part of the standard library is located in the `graphics` package (i.e. not in `core`), and contains the classes `graphics.Color` and `graphics.Image`.

## Color

The `graphics.Color` class stores a color in RGB + alpha format. Each component is stored as a 32-bit `Float` in the range 0-1. The alpha component is in the range from 0 (transparent) to 1 (opaque). The alpha component is not premultiplied in the `Color` class.

The `Color` class contains constructors that allow creating colors from color components (both with and without alpha), both as `Floats` and as `Bytes` (range 0-255). It also contains operators `+` and `-` for mixing colors, and `*` and `/` for scaling the color values (except alpha) with a scalar.

There are also convenience functions for creating various colors:

- `graphics.Color transparent()` - Transparent color, based on the color black.
- `graphics.Color black()`
- `graphics.Color white()`
- `graphics.Color red()`
- `graphics.Color green()`
- `graphics.Color blue()`
- `graphics.Color yellow()`
- `graphics.Color cyan()`
- `graphics.Color pink()`

The presentation library contains definitions of the DVIPS colors in the package `presentation.colors`.

## Image

The `graphics.Image` class stores an image in RGB + alpha format. Conceptually, the image consists of a 2D-array of `graphics.Color`s. For increased efficiency, however, each image is stored as a 32-bit RGBA value (i.e. 8 bits per channel). In C++, the `Image` provides access to the underlying buffer in a format that is often efficient for other libraries to consume. It also allows accessing individual pixels from Storm.

The interface is as follows:

- `init()`

  Empty image.

- `init(core.geometry.Size size)`

  Create an image with the specified size. The image is initially entirely transparent (based on the color black).

- `init(core.Nat w, core.Nat h)`

  Create an image with the specified size. The image is initially entirely transparent (based on the color black).

- `graphics.Color get(core.Nat x, core.Nat y)`

  Get the pixel at a coordinate.

- `graphics.Color get(core.geometry.Point at)`

  Get the pixel at the specified coordinate. Truncates any fractions, does not perform any interpolation.

- `void set(core.Nat x, core.Nat y, graphics.Color c)`

  Set the pixel at the specified coordinate.

- `void set(core.geometry.Point p, graphics.Color c)`

  Set the pixel at the specified coordinate.

- `core.Bool hasAlpha()`

  Does this image contain any pixels that are transparent or semi-transparent?

## ImageSet

The `graphics.ImageSet` class stores a set of images of different resolution that are related to each other. The intended use is to store multiple variants of an icon for an application, for example.

The interface is as follows:

- `init()`

  Create an empty image set.

- `init(graphics.Image image)`

  Create an image set with one element in it.

- `void push(graphics.Image image)`

  Add a new image.

- `graphics.ImageSet <<(graphics.Image image)`

  Add a new image.

- `core.Bool empty()`

  Empty?

- `core.Bool any()`

  Any?

- `core.Nat count()`

  Get the number of images in the set.

- `graphics.Image [](core.Nat i)`

  Get image number i.

- `graphics.Image closest(core.geometry.Size size)`

  Find the image closest to the specified dimensions.

- `graphics.Image closest(core.Nat width, core.Nat height)`

  Find the image closest to the specified dimensions.

- `graphics.Image atLeast(core.geometry.Size size)`

  Find the nerast larger (or equal) image to the specified dimensions.

- `graphics.Image atLeast(core.Nat width, core.Nat height)`

  Find the nerast larger (or equal) image to the specified dimensions.