Skip to content

Apply RGB renderer

View on GitHub

Apply an RGB renderer to a raster layer to enhance feature visibility.

Image of Apply RGB renderer sample

Use case

An RGB renderer is used to adjust the color bands of a multispectral image. Remote sensing images acquired from satellites often contain values representing the reflection of multiple spectrums of light. Changing the RGB renderer of such rasters can be used to differentiate and highlight particular features that reflect light differently, such as different vegetation types, or turbidity in water.

How to use the sample

Choose one of the stretch parameter types. The other options will adjust based on the chosen type. Add your inputs and select the 'Update' button to update the renderer.

How it works

  1. Create a Raster from a from a multispectral raster file.
  2. Create a RasterLayer from the raster.
  3. Create a Basemap from the raster layer and set it to the map.
  4. Create an RGBRenderer, specifying the StretchParameters and other properties.
  5. Set the Renderer on the raster layer.

Relevant API

  • Basemap
  • Raster
  • RasterLayer
  • RGBRenderer
  • StretchParameters

About the data

The raster used in this sample shows an area in the south of the Shasta-Trinity National Forest, California.

Tags

analysis, color, composite, imagery, multiband, multispectral, pan-sharpen, photograph, raster, spectrum, stretch, visualization

Sample Code

ApplyRGBRendererView.swiftApplyRGBRendererView.swiftApplyRGBRendererView.RangeSlider.swiftApplyRGBRendererView.SettingsView.swift
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2025 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import ArcGIS
import SwiftUI

struct ApplyRGBRendererView: View {
    /// A map with a raster layer.
    @State private var map: Map = {
        let raster = Raster(
            fileURL: Bundle.main.url(
                forResource: "Shasta",
                withExtension: "tif",
                subdirectory: "raster-file/raster-file"
            )!
        )
        return Map(basemap: Basemap(baseLayer: RasterLayer(raster: raster)))
    }()
    /// The RGB renderer settings.
    @State private var rendererSettings = RendererSettings()
    /// A Boolean value indicating whether the settings view should be presented.
    @State private var isShowingSettings = false
    /// The raster layer to apply RGB renderer.
    private var rasterLayer: RasterLayer {
        map.basemap?.baseLayers[0] as! RasterLayer
    }

    var body: some View {
        MapView(map: map)
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button("Stretch Parameter Settings") {
                        isShowingSettings = true
                    }
                    .popover(isPresented: $isShowingSettings) {
                        NavigationStack {
                            SettingsView(settings: $rendererSettings)
                                .onChange(of: rendererSettings, initial: true) {
                                    switch rendererSettings.stretchType {
                                    case .histogramEqualization:
                                        // Nothing to configure for histogram equalization.
                                        // It is useful when there are a lot of pixel values
                                        // that are closely grouped together
                                        setStretchParameters(HistogramEqualizationStretchParameters())
                                    case .minMax:
                                        // The pixel values which serve as endpoints for
                                        // the histogram used for the stretch.
                                        setStretchParameters(MinMaxStretchParameters(minValues: rendererSettings.minColor.rgb, maxValues: rendererSettings.maxColor.rgb))
                                    case .percentClip:
                                        // The percentile cutoff above which pixel values
                                        // in the raster dataset are to be clipped.
                                        setStretchParameters(PercentClipStretchParameters(min: rendererSettings.minValue, max: rendererSettings.maxValue))
                                    case .standardDeviation:
                                        // It applies a linear stretch between the
                                        // values defined by the standard deviation.
                                        setStretchParameters(StandardDeviationStretchParameters(factor: rendererSettings.standardDeviation))
                                    }
                                }
                        }
                        .presentationDetents([.fraction(0.5)])
                        .frame(idealWidth: 320, idealHeight: 360)
                    }
                }
            }
    }

    /// Sets the stretch parameters for the raster layer.
    /// - Parameter parameters: The stretch parameters to apply.
    private func setStretchParameters(_ parameters: StretchParameters) {
        rasterLayer.renderer = RGBRenderer(
            stretchParameters: parameters,
            bandIndexes: [],
            gammas: [],
            estimatesStatistics: true
        )
    }

    /// The settings for a RGB renderer.
    struct RendererSettings: Equatable {
        /// The stretch type to apply to the raster layer.
        var stretchType: StretchType = .histogramEqualization
        /// The color to use for the minimum values in the min-max stretch.
        var minColor: Color = .black
        /// The color to use for the maximum values in the min-max stretch.
        var maxColor: Color = .green
        /// The minimum value for the percent clip stretch.
        var minValue: Double = 20
        /// The maximum value for the percent clip stretch.
        var maxValue: Double = 80
        /// The standard deviation factor for the standard deviation stretch.
        var standardDeviation = 0.5
    }

    enum StretchType: CaseIterable {
        case histogramEqualization, minMax, percentClip, standardDeviation

        /// The label for the stretch type.
        var label: String {
            switch self {
            case .histogramEqualization: "Histogram Equalization"
            case .minMax: "Min-Max"
            case .percentClip: "Percent Clip"
            case .standardDeviation: "Standard Deviation"
            }
        }
    }
}

private extension Color {
    /// The RGB components of the color.
    var rgb: [Double] {
        UIColor(self)
            .cgColor
            .components!
            .prefix(3)
            .map { Double($0) * 255 }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.