## 15.2. Conversion Formulas

| editing-note
(Bill) These Conversion Formulas will likely move to Section 2.7 Fixed-Point Data Conversions (RGB to sRGB and sRGB to RGB) and section 2.6 Numeric Representation and Computation (RGB to Shared Exponent and Shared Exponent to RGB)

### 15.2.1. RGB to Shared Exponent Conversion

An RGB color (red, green, blue) is transformed to a shared exponent color (redshared, greenshared, blueshared, expshared) as follows:

First, the components (red, green, blue) are clamped to (redclamped, greenclamped, blueclamped) as:

  redclamped = max(0, min(sharedexpmax, red))
  greenclamped = max(0, min(sharedexpmax, green))
  blueclamped = max(0, min(sharedexpmax, blue))

Where:

\begin{align*} N & = 9 & \textrm{number of mantissa bits per component} \\ B & = 15 & \textrm{exponent bias} \\ E_{max} & = 31 & \textrm{maximum possible biased exponent value} \\ sharedexp_{max} & = \frac{(2^N-1)}{2^N} \times 2^{(E_{max}-B)} \end{align*}

| Note
NaN, if supported, is handled as in IEEE 754-2008 `minNum()` and `maxNum()`. That is the result is a NaN is mapped to zero.

The largest clamped component, maxclamped is determined:

  maxclamped = max(redclamped, greenclamped, blueclamped)

A preliminary shared exponent exp' is computed:

\begin{align*} exp' = \begin{cases} \left \lfloor \log_2(max_{clamped}) \right \rfloor + (B+1) & \textrm{for } max_{clamped} > 2^{-(B+1)} \\ 0 & \textrm{for } max_{clamped} \leq 2^{-(B+1)} \end{cases} \end{align*}

The shared exponent expshared is computed:

\begin{align*} max_{shared} = \left \lfloor \frac{max_{clamped}}{2^{(exp'-B-N)}}+\frac{1}{2} \right \rfloor \end{align*}
\begin{align*} exp_{shared} = \begin{cases} exp' & \textrm{for } 0 \leq max_{shared} < 2^N \\ exp'+1 & \textrm{for } max_{shared} = 2^N \end{cases} \end{align*}

Finally, three integer values in the range 0 to 2N are computed:

\begin{align*} red_{shared} & = \left \lfloor \frac{red_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} \right \rfloor \\ green_{shared} & = \left \lfloor \frac{green_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} \right \rfloor \\ blue_{shared} & = \left \lfloor \frac{blue_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} \right \rfloor \end{align*}

### 15.2.2. Shared Exponent to RGB

A shared exponent color (redshared, greenshared, blueshared, expshared) is transformed to an RGB color (red, green, blue) as follows:

  red = redshared × 2(expshared - B - N)
  green = greenshared × 2(expshared - B - N)
  blue = blueshared × 2(expshared - B - N)

Where:

  N = 9 (number of mantissa bits per component)
  B = 15 (exponent bias)