Color Space Gamma Calculator
Shaders & Graphics Boilerplate Kit
Companion workbook & kit for Color Space Gamma Calculator
- WebGL shader templates
To convert gamma-encoded sRGB values (0–255) to linear RGB for physically based shaders, divide each channel by 255 and raise to the 2.2 gamma exponent (C_linear = (C_srgb / 255)^2.2).
How viafinds compares
| Capability | viafinds |
|---|---|
| Graphics Pipeline Focus | Built specifically for RGB color spaces, PBR uniforms, and Rec. 709 luminance |
| Simultaneous 3-Channel RGB Calculation | Computes Red, Green, Blue, Luminance, and 8-bit quantized outputs together |
| Bidirectional Decode & Encode Toggle | One-click toggle between sRGB-to-Linear (γ) and Linear-to-sRGB (1/γ) |
| Ad Experience & Execution Privacy | Zero display ads, zero tracking payloads, 100% browser-local math |
| Shader Integration Companion | Pairs directly with ready-to-compile WebGL gamma-corrected GLSL templates |
Why Gamma Correction Matters in Real-Time Graphics
Every physically based rendering (PBR) pipeline, whether built in WebGL, WebGPU, Vulkan, DirectX, or Unity, relies on one fundamental rule: light math must happen in linear color space, while storage and display happen in gamma-encoded color space. If you pass raw 8-bit sRGB texture samples or CSS hex colors straight into a fragment shader's dot-product lighting equation, specular highlights blow out, shadows crush unevenly, and alpha-blended edges develop dark halos.
Gamma encoding exists because human vision does not perceive brightness linearly. According to the Weber-Fechner law, our eyes are far more sensitive to small differences in dark tones than in bright tones. If an 8-bit image (0 to 255 per channel) stored linear light intensity directly, roughly half of all 256 code values would be wasted on bright highlights that look nearly identical to the human eye, while dark shadows would suffer from severe color banding. By applying a nonlinear power curve (gamma encoding) before saving textures or PNGs, engineers allocate more digital precision to dark values where human vision needs it most.
The Core Gamma Transformation Formulas
Graphics developers use two primary mathematical models for gamma conversion: the standard 2.2 power-law approximation (used in this calculator for rapid shader prototyping) and the exact piecewise IEC 61966-2-1 sRGB transfer function.
1. Standard 2.2 Power-Law Formulas
First, normalize any 8-bit RGB channel value (0–255) into a floating-point scalar (0.0–1.0):
C_norm = C_8bit / 255
To decode an sRGB or gamma-encoded color into linear space before lighting calculations (Electro-Optical Transfer Function, or EOTF):
C_linear = (C_norm) ^ γ (where γ = 2.2 by default)
To encode a computed linear light value back into display-ready sRGB space at the end of a post-processing pass (Opto-Electronic Transfer Function, or OETF):
C_srgb = (C_linear) ^ (1 / γ) (where 1 / 2.2 ≈ 0.4545)
2. Exact Piecewise IEC 61966-2-1 sRGB Specification
Hardware texture samplers (GL_SRGB8_ALPHA8) and modern display controllers implement a small linear toe near black to avoid an infinite slope at zero during differentiation:
- Decode (sRGB to Linear):
- If
C_srgb <= 0.04045:C_linear = C_srgb / 12.92 - Else:
C_linear = ((C_srgb + 0.055) / 1.055) ^ 2.4
- If
- Encode (Linear to sRGB):
- If
C_linear <= 0.0031308:C_srgb = 12.92 * C_linear - Else:
C_srgb = 1.055 * (C_linear ^ (1 / 2.4)) - 0.055
- If
Across almost the entire [0, 1] range, the 2.2 power-law curve closely tracks the piecewise IEC curve within a fraction of a percent, making pow(color, vec3(2.2)) and pow(color, vec3(1.0 / 2.2)) the standard fast path in mobile and WebGL shaders.
3. Relative Linear Luminance (Rec. 709 / sRGB)
Human eyes perceive green light as significantly brighter than red or blue at equal physical intensity. Once your RGB channels are in linear space, compute physically accurate relative luminance Y using the Rec. 709 primaries:
Y = 0.2126 * R_linear + 0.7152 * G_linear + 0.0722 * B_linear
Never apply luminance weights to gamma-encoded sRGB channels directly; doing so overweights dark channels and skews tone-mapping operators such as Reinhard or ACES Filmic.
Worked Examples for Graphics Programmers
Example 1: Why Mid-Gray (RGB 128, 128, 128) Is Not 50% Light
Suppose a UI designer hands you #808080 (R=128, G=128, B=128) for a neutral PBR albedo swatch.
- Normalize:
128 / 255 = 0.50196 - Decode with γ = 2.2:
0.50196 ^ 2.2 = 0.2195
Even though 128 sits halfway through the 8-bit integer range, #808080 reflects only 21.95% of incoming physical light—very close to the classic 18% photographic gray card. If you accidentally pass 0.502 into a shader without gamma decoding, your surface reflects more than twice the intended energy.
Example 2: Converting a Hex Uniform (#E6732D) for a Fragment Shader
When uploading a brand or emissive hex color as a vec3 shader uniform, convert each two-character hex byte to decimal first (0xE6 = 230, 0x73 = 115, 0x2D = 45), then enter (230, 115, 45) into the calculator in Decode mode:
- Red:
(230 / 255) ^ 2.2 = (0.90196) ^ 2.2 = 0.7969 - Green:
(115 / 255) ^ 2.2 = (0.45098) ^ 2.2 = 0.1735 - Blue:
(45 / 255) ^ 2.2 = (0.17647) ^ 2.2 = 0.0221 - Linear Luminance:
(0.2126 * 0.7969) + (0.7152 * 0.1735) + (0.0722 * 0.0221) = 0.2951
Notice how dramatically the green and blue channels drop relative to red once linearized. Passing vec3(0.7969, 0.1735, 0.0221) into your GLSL uniform preserves the exact warm orange hue under HDR bloom and tone mapping.
Example 3: Encoding Linear Render Target Output Back to Display sRGB
Imagine your deferred lighting pass computes a linear pixel value of 0.2195 (56 if naively multiplied by 255). Toggle Direction to On (Encode) in the calculator with input 56 (56 / 255 = 0.2196) and γ = 2.2:
0.2196 ^ (1 / 2.2) = 0.2196 ^ 0.4545 = 0.5020 -> round(255 * 0.5020) = 128
The encode step restores the value back to 128, ensuring the monitor's native ~2.2 electro-optical curve displays the exact linear light intensity your shader calculated.
Pipeline Checklist: What Gets Gamma-Decoded and What Does Not
One of the most common bugs in 3D engines is applying gamma decoding to non-color data textures:
- Always decode to linear (use sRGB texture format or
pow(x, 2.2)): Albedo/base color maps, diffuse textures, emissive maps, and artist-authored UI colors. - Never gamma-decode (keep strictly linear): Normal maps, metallic masks, roughness maps, ambient occlusion maps, height/displacement maps, and HDR environment cubemaps (
.hdr/.exrare already linear). - Alpha channel: Opacity (
AinRGBA) is always stored linearly, even insideGL_SRGB8_ALPHA8textures.
100% Browser-Local Privacy Guarantee
This Color Space Gamma Calculator executes every mathematical operation locally inside your browser's JavaScript runtime. No color values, shader parameters, or session inputs are transmitted to external servers, logged in analytics databases, or stored remotely. If you need custom enterprise pipeline tooling or want to suggest additional color spaces such as OKLab or ACEScg, reach out via our contact page or explore more browser-local utilities on the home page.
User Reviews
No reviews yet. Be the first to share feedback below.