How Does Google Color Picker Convert HEX, RGB, CMYK, and HSL?
The interactive Color Picker widget in Google Search converts between HEX, RGB, CMYK, and HSL in real time using standard mathematical color-space formulas executed directly in the browser. Because digital screens display colors using red, green, and blue light, the widget treats RGB as its central hub, translating any edited value into normalized RGB coordinates before recalculating the remaining formats.
RGB as the Central Hub
Every color in the widget is represented internally by three primary channels: Red (\(R\)), Green (\(G\)), and Blue (\(B\)), each ranging from \(0\) to \(255\). For most mathematical conversions, these integer values are first normalized to a decimal range between \(0\) and \(1\):
\[R' = \frac{R}{255}, \quad G' = \frac{G}{255}, \quad B' = \frac{B}{255}\]
Using normalized values allows the widget to apply consistent formulas when switching between additive (RGB, HEX, HSL) and subtractive (CMYK) color models.
Converting Between HEX and RGB
Hexadecimal (HEX) codes are simply a base-16 representation of the
three 8-bit RGB channels. A six-character code like #RRGGBB
splits into three two-digit pairs:
- HEX to RGB: Each two-digit hexadecimal pair is
converted to a base-10 integer between \(0\) and \(255\). For example,
FFequals \(15 \times 16^1 + 15 \times 16^0 = 255\). - RGB to HEX: Each decimal channel value is divided by \(16\) to find the first hex digit (the quotient) and the second hex digit (the remainder), padded with a leading zero if the value is below \(16\).
Converting Between RGB and HSL
HSL (Hue, Saturation, Lightness) describes color in a cylindrical coordinate system rather than a cube. To convert normalized RGB (\(R', G', B'\)) to HSL, the widget first finds the maximum and minimum channel values:
\[C_{\max} = \max(R', G', B'), \quad C_{\min} = \min(R', G', B'), \quad \Delta = C_{\max} - C_{\min}\]
From these, it calculates:
- Lightness (\(L\)): \[L = \frac{C_{\max} + C_{\min}}{2}\]
- Saturation (\(S\)): If \(\Delta = 0\), \(S = 0\). Otherwise: \[S = \frac{\Delta}{1 - |2L - 1|}\]
- Hue (\(H\)):
Measured in degrees (\(0^\circ\) to
\(360^\circ\)), depending on which
channel is \(C_{\max}\):
- If \(C_{\max} = R'\): \(H = 60^\circ \times \left(\frac{G' - B'}{\Delta} \bmod 6\right)\)
- If \(C_{\max} = G'\): \(H = 60^\circ \times \left(\frac{B' - R'}{\Delta} + 2\right)\)
- If \(C_{\max} = B'\): \(H = 60^\circ \times \left(\frac{R' - G'}{\Delta} + 4\right)\)
To convert HSL back to RGB, the widget reverses the process by computing chroma \(C = (1 - |2L - 1|) \times S\), an intermediate value \(X = C \times (1 - |(H / 60^\circ) \bmod 2 - 1|)\), and a lightness offset \(m = L - \frac{C}{2}\), then mapping \((C, X, 0)\) to \((R', G', B')\) based on the \(60^\circ\) sector of \(H\).
Converting Between RGB and CMYK
CMYK (Cyan, Magenta, Yellow, Key/Black) is a subtractive model used in printing. Because it does not account for specific printer ink profiles, the widget uses a standard device-independent approximation.
To convert normalized RGB to CMYK, it first determines the black key (\(K\)):
\[K = 1 - \max(R', G', B')\]
If \(K = 1\) (pure black), then \(C = M = Y = 0\). Otherwise, the remaining channels are calculated as:
\[C = \frac{1 - R' - K}{1 - K}, \quad M = \frac{1 - G' - K}{1 - K}, \quad Y = \frac{1 - B' - K}{1 - K}\]
To convert CMYK back to RGB, the widget multiplies each ink value against the remaining lightness:
\[R = 255 \times (1 - C) \times (1 - K)\] \[G = 255 \times (1 - M) \times (1 - K)\] \[B = 255 \times (1 - Y) \times (1 - K)\]
Real-Time Updates and Rounding
Because all of these equations are lightweight arithmetic operations, JavaScript inside the Google Search widget evaluates them instantaneously on every slider drag or text edit. Values are rounded to the nearest integer (or percentage) for display, which can occasionally cause minor one-unit shifts when converting back and forth between formats like CMYK and RGB.