Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

Exposure, contrast and saturation in WebGL

Tune Esri satellite imagery exposure, contrast and saturation live on the GPU in OpenLayers using reactive WebGLTileLayer style variables.

Esri's imagery of central Kraków with exposure 0.10, contrast 0.20 and saturation 0.40: the Vistula green, roofs and fields vivid, and the three sliders below.
A screenshot of the example. Imagery: Esri World Imagery (Esri, Maxar, Earthstar Geographics and the GIS User Community).

What this example shows

Over central Kraków on Esri World Imagery, three range sliders (Exposure, Contrast and Saturation, each spanning -0.5 to 0.5 in 0.01 increments from 0) grade the satellite backdrop smoothly at full GPU frame rate without re-fetching tiles or recompiling shaders.

When you need it

Bright, saturated aerial photography often visually overwhelms thin cadastral lines, utility networks or point labels drawn above it.

Desaturating, darkening or flattening imagery directly in a WebGL fragment shader lets users tune backdrop legibility instantaneously on both desktop and mobile GPUs.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

webgl-tile-style.shared.ts
import type { Style } from 'ol/layer/WebGLTile.js';

export type Adjustment = 'exposure' | 'contrast' | 'saturation';

export const ADJUSTMENTS: Adjustment[] = ['exposure', 'contrast', 'saturation'];

/** Exposure, contrast and saturation of the imagery, each read from a style variable. */
export const STYLE: Style = {
  exposure: ['var', 'exposure'],
  contrast: ['var', 'contrast'],
  saturation: ['var', 'saturation'],
};
webgl-tile-style.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  MapComponent,
  SourceImageTileComponent,
  ViewComponent,
} from 'cc-openlayers';
import { LayerWebGLTileComponent } from 'cc-openlayers/webgl';
import { ADJUSTMENTS, type Adjustment, STYLE } from './webgl-tile-style.shared';

/**
 * Esri's World Imagery over Kraków drawn by a WebGL tile layer whose style adjusts exposure, contrast and saturation
 * through variables: moving a slider updates the variable in place, without rebuilding the style.
 */
@Component({
  selector: 'app-webgl-tile-style',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerWebGLTileComponent,
    MapComponent,
    SourceImageTileComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="13">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-webgltile [style]="style" [variables]="variables()">
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="19"
          attributions="Tiles © Esri — Source: Esri, Maxar, Earthstar Geographics, and the GIS User Community"
        />
      </aol-layer-webgltile>
    </aol-map>
    <div class="example-controls">
      @for (adjustment of adjustments; track adjustment) {
        <label>
          {{ adjustment }}
          <input
            type="range"
            min="-0.5"
            max="0.5"
            step="0.01"
            [name]="adjustment"
            [value]="variables()[adjustment]"
            (input)="adjust(adjustment, $event)"
          />
          <output>{{ variables()[adjustment].toFixed(2) }}</output>
        </label>
      }
    </div>
  `,
})
export class WebglTileStyle {
  readonly variables = signal<Record<Adjustment, number>>({ exposure: 0, contrast: 0, saturation: 0 });
  protected readonly adjustments = ADJUSTMENTS;
  protected readonly style = STYLE;

  protected adjust(adjustment: Adjustment, event: Event): void {
    const value = (event.target as HTMLInputElement).valueAsNumber;
    this.variables.update((variables) => ({ ...variables, [adjustment]: value }));
  }
}

Architecture and OpenLayers mechanics

On aol-layer-webgltile, [style] binds exposure, contrast and saturation to ['var', 'exposure'], ['var', 'contrast'] and ['var', 'saturation']. Passing slider values through [variables]="variables()" invokes OpenLayers’ fast uniform update (updateStyleVariables) on every drag frame while keeping the compiled WebGL program intact.

How it works

  • Layer style properties exposure, contrast and saturation reference ['var', 'exposure'], ['var', 'contrast'] and ['var', 'saturation'].
  • Updating [variables]="variables()" mutates GPU shader uniforms via updateStyleVariables without rebuilding the style object.
  • All three sliders run from -0.5 to 0.5 (step 0.01) starting at 0 (unmodified imagery).

Pitfalls

  • Constructing and passing a brand-new style object literal on every slider tick forces OpenLayers to recompile WebGL shaders dozens of times per second—keep [style] static and mutate [variables] instead.
  • Shader colour grading alters screen pixels only; calling layer.getData(pixel) still returns the unmodified source tile RGB values.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.