Set brightness, constrast, and saturation on color layers and maps.

Parameters
100% © IGN, Métropole Grand Lyon
index.js
import { Vector3 } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";

import { Fill, Stroke, Style } from "ol/style.js";
import GeoJSON from "ol/format/GeoJSON.js";

import Instance from "@giro3d/giro3d/core/Instance.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import WmsSource from "@giro3d/giro3d/sources/WmsSource.js";
import VectorSource from "@giro3d/giro3d/sources/VectorSource.js";

function bindSlider(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLInputElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.valueAsNumber);
  };

  const setValue = (v, min, max, step) => {
    if (min != null && max != null) {
      element.min = min.toString();
      element.max = max.toString();

      if (step != null) {
        element.step = step;
      }
    }
    element.valueAsNumber = v;
    onChange(element.valueAsNumber);
  };

  const initialValue = element.valueAsNumber;

  return [setValue, initialValue, element];
}

function bindButton(id, onClick) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLButtonElement)) {
    throw new Error(
      "invalid binding element: expected HTMLButtonElement, got: " +
        element.constructor.name,
    );
  }

  element.onclick = () => {
    onClick(element);
  };

  return element;
}

function bindDropDown(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLSelectElement)) {
    throw new Error(
      "invalid binding element: expected HTMLSelectElement, got: " +
        element.constructor.name,
    );
  }

  element.onchange = () => {
    onChange(element.value);
  };

  const callback = (v) => {
    element.value = v;
    onChange(element.value);
  };

  const setOptions = (options) => {
    const items = options.map(
      (opt) =>
        `<option value=${opt.id} ${opt.selected ? "selected" : ""}>${opt.name}</option>`,
    );
    element.innerHTML = items.join("\n");
  };

  return [callback, element.value, element, setOptions];
}

Instance.registerCRS(
  "EPSG:3946",
  "+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
);
Instance.registerCRS(
  "EPSG:4171",
  "+proj=longlat +ellps=GRS80 +no_defs +type=crs",
);

const instance = new Instance({
  target: "view",
  crs: "EPSG:3946",
});

const xmin = 1837816.94334;
const xmax = 1847692.32501;
const ymin = 5170036.4587;
const ymax = 5178412.82698;

const extent = new Extent("EPSG:3946", xmin, xmax, ymin, ymax);

const map = new Map({ extent });
instance.add(map);

const satelliteSource = new WmsSource({
  url: "https://data.geopf.fr/wms-r",
  projection: "EPSG:3946",
  layer: "HR.ORTHOIMAGERY.ORTHOPHOTOS",
  imageFormat: "image/jpeg",
});

const satellite = new ColorLayer({
  name: "satellite",
  source: satelliteSource,
  extent: map.extent,
});
map.addLayer(satellite);

// Adds our first layer from a geojson file
// Initial source: https://data.grandlyon.com/jeux-de-donnees/parcs-places-jardins-indice-canopee-metropole-lyon/info
const geoJsonLayer = new ColorLayer({
  name: "geojson",
  source: new VectorSource({
    data: {
      url: "https://3d.oslandia.com/lyon/evg_esp_veg.evgparcindiccanope_latest.geojson",
      format: new GeoJSON(),
    },
    // Defines the dataProjection to reproject the data,
    // GeoJSON specifications say that the crs should be EPSG:4326 but
    // here we are using a different one.
    dataProjection: "EPSG:4171",
    style: (feature) =>
      new Style({
        fill: new Fill({
          color: `rgba(0, 128, 0, ${feature.get("indiccanop")})`,
        }),
        stroke: new Stroke({
          color: "white",
        }),
      }),
  }),
});
map.addLayer(geoJsonLayer);

const camera = instance.view.camera;
const cameraAltitude = 2000;

const center = extent.centerAsVector3();

const cameraPosition = new Vector3(center.x, center.y, cameraAltitude);
camera.position.copy(cameraPosition);

const controls = new MapControls(camera, instance.domElement);
controls.target = center;

controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.maxPolarAngle = Math.PI / 2.3;

controls.saveState();

instance.view.setControls(controls);

const [setSatelliteBrightness] = bindSlider("satellite-brightness", (v) => {
  satellite.brightness = v;
  instance.notifyChange(map);
});
const [setSatelliteContrast] = bindSlider("satellite-contrast", (v) => {
  satellite.contrast = v;
  instance.notifyChange(map);
});
const [setSatelliteSaturation] = bindSlider("satellite-saturation", (v) => {
  satellite.saturation = v;
  instance.notifyChange(map);
});
const [setVectorBrightness] = bindSlider("vector-brightness", (v) => {
  geoJsonLayer.brightness = v;
  instance.notifyChange(map);
});
const [setVectorContrast] = bindSlider("vector-contrast", (v) => {
  geoJsonLayer.contrast = v;
  instance.notifyChange(map);
});
const [setVectorSaturation] = bindSlider("vector-saturation", (v) => {
  geoJsonLayer.saturation = v;
  instance.notifyChange(map);
});

const mapParams = map.colorimetry;

const [setMapBrightness] = bindSlider("map-brightness", (v) => {
  mapParams.brightness = v;
  instance.notifyChange(map);
});
const [setMapContrast] = bindSlider("map-contrast", (v) => {
  mapParams.contrast = v;
  instance.notifyChange(map);
});
const [setMapSaturation] = bindSlider("map-saturation", (v) => {
  mapParams.saturation = v;
  instance.notifyChange(map);
});

bindButton("reset", () => {
  setMapBrightness(0);
  setMapContrast(1);
  setMapSaturation(1);

  setVectorBrightness(0);
  setVectorContrast(1);
  setVectorSaturation(1);

  setSatelliteBrightness(0);
  setSatelliteContrast(1);
  setSatelliteSaturation(1);

  instance.notifyChange(map);
});

bindDropDown("layer-select", (selectedValue) => {
  document.getElementById("map-settings").style.display = "none";
  document.getElementById("satellite-settings").style.display = "none";
  document.getElementById("geojson-settings").style.display = "none";

  if (selectedValue === "map") {
    document.getElementById("map-settings").style.display = "block";
  } else if (selectedValue === "satellite") {
    document.getElementById("satellite-settings").style.display = "block";
  } else if (selectedValue === "geojson") {
    document.getElementById("geojson-settings").style.display = "block";
  }
});

Inspector.attach("inspector", instance);
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Color Adjustment</title>
    <meta charset="UTF-8" />
    <meta name="name" content="color_adjustments" />
    <meta
      name="description"
      content="Set brightness, constrast, and saturation on color layers and maps."
    />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="icon" href="https://giro3d.org/images/favicon.svg" />
    <link
      href="https://giro3d.org/assets/bootstrap-custom.css"
      rel="stylesheet"
    />
    <script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://giro3d.org/latest/examples/css/example.css"
    />
  </head>

  <body>
    <div id="view" class="m-0 p-0 w-100 h-100"></div>
    <div
      id="inspector"
      class="position-absolute top-0 start-0 mh-100 overflow-auto"
    ></div>

    <div class="side-pane-with-status-bar">
      <div class="card">
        <div class="card-header">
          Parameters
          <button
            type="button"
            id="reset"
            class="btn btn-sm btn-primary rounded float-end"
          >
            reset
          </button>
        </div>
        <div class="card-body">
          <select class="form-select" id="layer-select">
            <option value="map">Map</option>
            <option value="satellite">Satellite layer</option>
            <option value="geojson">GeoJSON layer</option>
          </select>

          <div id="map-settings">
            <!-- Map parameters -->
            <form>
              <label for="map-brightness" class="form-label">Brightness</label>
              <div class="input-group">
                <input
                  type="range"
                  min="-1"
                  max="1"
                  value="0"
                  class="form-range"
                  step="0.05"
                  id="map-brightness"
                  autocomplete="off"
                />
              </div>

              <label for="map-contrast" class="form-label">Contrast</label>
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="map-contrast"
                  autocomplete="off"
                />
              </div>

              <label for="map-saturation" class="form-label">Saturation</label>
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="map-saturation"
                  autocomplete="off"
                />
              </div>
            </form>
          </div>
          <div id="satellite-settings" style="display: none">
            <!-- Satellite layer parameters -->
            <form>
              <label for="satellite-brightness" class="form-label"
                >Brightness</label
              >
              <div class="input-group">
                <input
                  type="range"
                  min="-1"
                  max="1"
                  value="0"
                  class="form-range"
                  step="0.05"
                  id="satellite-brightness"
                  autocomplete="off"
                />
              </div>

              <label for="satellite-contrast" class="form-label"
                >Contrast</label
              >
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="satellite-contrast"
                  autocomplete="off"
                />
              </div>

              <label for="satellite-saturation" class="form-label"
                >Saturation</label
              >
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="satellite-saturation"
                  autocomplete="off"
                />
              </div>
            </form>
          </div>
          <div id="geojson-settings" style="display: none">
            <!-- Vector layer parameters -->
            <form>
              <label for="vector-brightness" class="form-label"
                >Brightness</label
              >
              <div class="input-group">
                <input
                  type="range"
                  min="-1"
                  max="1"
                  value="0"
                  class="form-range"
                  step="0.05"
                  id="vector-brightness"
                  autocomplete="off"
                />
              </div>

              <label for="vector-contrast" class="form-label">Contrast</label>
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="vector-contrast"
                  autocomplete="off"
                />
              </div>

              <label for="vector-saturation" class="form-label"
                >Saturation</label
              >
              <div class="input-group">
                <input
                  type="range"
                  min="0"
                  max="3"
                  value="1"
                  class="form-range"
                  step="0.05"
                  id="vector-saturation"
                  autocomplete="off"
                />
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

    <script type="module" src="index.js"></script>
    <script>
      /* activate popovers */
      const popoverTriggerList = [].slice.call(
        document.querySelectorAll('[data-bs-toggle="popover"]'),
      );
      popoverTriggerList.map(
        // bootstrap is used as script in the template, disable warning about undef
        // eslint-disable-next-line no-undef
        (popoverTriggerEl) =>
          new bootstrap.Popover(popoverTriggerEl, {
            trigger: "hover",
            placement: "left",
            content: document.getElementById(
              popoverTriggerEl.getAttribute("data-bs-content"),
            ).innerHTML,
            html: true,
          }),
      );
    </script>
  </body>
</html>
package.json
{
    "name": "color_adjustments",
    "dependencies": {
        "@giro3d/giro3d": "0.41.0"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}