## 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 $(red_{shared}, green_{shared}, blue_{shared}, exp_{shared})$ as follows:

First, the components $(red, green, blue)$ are clamped to $(red_{clamped}, green_{clamped}, blue_{clamped})$ as:

\begin{align*} red_{clamped} & = \max(0,min(sharedexp_{max},red)) \\ green_{clamped} & = \max(0,min(sharedexp_{max},green)) \\ blue_{clamped} & = \max(0,min(sharedexp_{max},blue)) \end{align*}

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, $max_{clamped}$ is determined:

\begin{align*} max_{clamped} = \max(red_{clamped},green_{clamped},blue_{clamped}) \end{align*}

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 $exp_{shared}$ 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 $2^N$ 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 $(red_{shared}, green_{shared}, blue_{shared}, exp_{shared})$ is transformed to an RGB color $(red, green, blue)$ as follows:

\begin{align*} red & = red_{shared}\times 2^{(exp_{shared}-B-N)} \\ green & = green_{shared}\times 2^{(exp_{shared}-B-N)} \\ blue & = blue_{shared}\times 2^{(exp_{shared}-B-N)} \\ \end{align*}

Where:

\begin{align*} N & = 9 & \textrm{number of mantissa bits per component} \\ B & = 15 & \textrm{exponent bias} \end{align*}