# `ImageDimensions`

An image's width and height in whole pixels, written `1920x1080`, each from 1 to 2,147,483,647. `media.fit` scales one to fit inside another.

[Media and visual](types.md#media-and-visual); not ordered; since Jadpo 0.1.0.

## Make one

Build one from its fields, `ImageDimensions { width: ...  height: ... }`.

`text.parse(raw, as: ImageDimensions)` reads one from its text; text that isn't one at all fails `malformed`, such as `0x1080`, `1920x0` or `1920X1080`.

This operation gives one: `media.fit`.

## Fields

| Field | Type |
|---|---|
| `width` | `Int` |
| `height` | `Int` |

## Text, JSON and storage

`text.from` writes its canonical text, the one form `text.parse` reads back. JSON sends an object of its parts.

| Text | JSON |
|---|---|
| `1x1` | `{"width":1,"height":1}` |
| `4000x3000` | `{"width":4000,"height":3000}` |
| `320x240` | `{"width":320,"height":240}` |
| `2147483647x1` | `{"width":2147483647,"height":1}` |

It is stored in 2 columns, each named for the field, `_` and its suffix. This is `320x240`:

| Column | Type | Holds |
|---|---|---|
| `<field>_width` | `integer` | `320` |
| `<field>_height` | `integer` | `240` |

## Shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `1x1` | `1x1` |
| `4000x3000` | `4000x3000` |
| `320x240` | `320x240` |
| `2147483647x1` | `2147483647x1` |

## Operations

- `media.fit(dimensions: ImageDimensions, within: ImageDimensions) -> ImageDimensions`: `dimensions` scaled to fit inside `within`, keeping its aspect ratio and never made larger, each side rounded down to a whole pixel and never below 1, as CSS's `object-fit: scale-down` fits an image: `media.fit(photo, within: tile)`.

Like every standard type, it also has the contract's own operations: `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. It isn't ordered, so `<` and sorting don't take it. [Every standard type meets one contract](../topics/every-standard-type-meets-one-contract.md) says how they work.
