About the Use an AlgorithmicColorRamp to color a ClassBreaksRenderer Sample
[C#]
frmAlgorithmicColorRamp.cs
//INSTANT C# NOTE: Formerly VB.NET project-level imports:
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using Microsoft.VisualBasic.Compatibility.VB6;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AlgorithmicColorRamp
{
internal partial class frmAlgorithmicColorRamp : System.Windows.Forms.Form
{
// This form allows a user to set properties determining the constraints of a
// AlgoritmicColorRamp, which is then used to populate an existing ClassBreaksRenderer
// on an existing FeatureLayer.
//
//
// The m_lngClasses variable is set by the calling function, to indicate the
// number of random colors required by the classbreaksrenderer.
//
public int m_lngClasses;
//
// The m_enumNewColors variable holds the colors to be returned to the calling function.
//
public IEnumColors m_enumNewColors;
//
// The m_lngColors variable holds the index of the last color displayed in the array.
//
private int m_lngColors;
private System.Windows.Forms.Button[] Buttons = new System.Windows.Forms.Button[3];
private System.Windows.Forms.Label[] LabelsIndex = new System.Windows.Forms.Label[11];
private System.Windows.Forms.TextBox[] TextBoxColors = new System.Windows.Forms.TextBox[11];
private void cmbAlgorithm_SelectedIndexChanged(object eventSender, System.EventArgs eventArgs)
{
if (cmbAlgorithm.SelectedIndex > -1)
UpdateRamp();
}
private void cmdCancel_Click(object eventSender, System.EventArgs eventArgs)
{
//
// User pressed Cancel, so we set the colors enumeration to nothing and
// unload the form.
//
m_enumNewColors = null;
this.Close();
}
private void cmdEnumColorsNext_Click(object eventSender, System.EventArgs eventArgs)
{
//
// Increase the indicator variable m_lngColors by 10, so we can display the
// next ten colors to the user.
//
if (m_enumNewColors != null)
{
m_lngColors = m_lngColors + 10;
HideColors();
LooRGBColors();
}
}
private void cmdEnumColorsFirst_Click(object eventSender, System.EventArgs eventArgs)
{
//
// Reset the indicator variable to zero.
//
if (m_enumNewColors != null)
{
m_lngColors = 0;
HideColors();
LooRGBColors();
}
}
private void cmdOK_Click(object eventSender, System.EventArgs eventArgs)
{
//
// Check we have a colors enumeration.
//
if (m_enumNewColors == null)
MessageBox.Show("You have not created a new color ramp." + "Your layer symbology will be unchanged.", "No Ramp Created", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
this.Hide();
}
private void frmAlgorithmicColorRamp_Load(object eventSender, System.EventArgs eventArgs)
{
//
// Initialize the controls.
//
SetupControls();
}
private void UpdateRamp()
{
//
// Create a new AlgorithmicColorRamp object, and get it's
// IAlgorithmicColorRamp interface.
//
IAlgorithmicColorRamp AlgortihmicColorRamp = null;
AlgortihmicColorRamp = new ESRI.ArcGIS.Display.AlgorithmicColorRamp();
//
// Set the size of the color ramp to the number of classes
// to be renderered.
//
AlgortihmicColorRamp.Size = m_lngClasses;
//
// Set the color ramps properties.
//
IRgbColor RGBColor = null;
RGBColor = new RgbColor();
RGBColor.RGB = System.Drawing.ColorTranslator.ToOle(txtStartColor.BackColor);
AlgortihmicColorRamp.FromColor = RGBColor;
RGBColor.RGB = System.Drawing.ColorTranslator.ToOle(txtEndColor.BackColor);
AlgortihmicColorRamp.ToColor = RGBColor;
AlgortihmicColorRamp.Algorithm = (esriColorRampAlgorithm)cmbAlgorithm.SelectedIndex;
bool boolRamp = false;
if (AlgortihmicColorRamp.Size > 0)
{
boolRamp = true;
AlgortihmicColorRamp.CreateRamp(out boolRamp);
if (boolRamp)
{
m_enumNewColors = AlgortihmicColorRamp.Colors;
m_enumNewColors.Reset();
cmdOK.Enabled = true;
//
// Check if we should be showing the colors.
//
if (chkShowColors.CheckState == System.Windows.Forms.CheckState.Checked)
{
//
// Populate the Colors textbox array and it's labels.
//
m_lngColors = 0;
ShowColorsArray();
}
}
}
}
private void SetupControls()
{
LabelsIndex[0] = Label1;
LabelsIndex[1] = Label2;
LabelsIndex[2] = Label3;
LabelsIndex[3] = Label4;
LabelsIndex[4] = Label5;
LabelsIndex[5] = Label6;
LabelsIndex[6] = Label7;
LabelsIndex[7] = Label8;
LabelsIndex[8] = Label9;
LabelsIndex[9] = Label10;
TextBoxColors[0] = TextBox1;
TextBoxColors[1] = TextBox2;
TextBoxColors[2] = TextBox3;
TextBoxColors[3] = TextBox4;
TextBoxColors[4] = TextBox5;
TextBoxColors[5] = TextBox6;
TextBoxColors[6] = TextBox7;
TextBoxColors[7] = TextBox8;
TextBoxColors[8] = TextBox9;
TextBoxColors[9] = TextBox10;
HideColors();
txtStartColor.Text = "";
txtEndColor.Text = "";
txtStartColor.BackColor = System.Drawing.ColorTranslator.FromOle(0XFF);
txtEndColor.BackColor = System.Drawing.ColorTranslator.FromOle(0XFF);
//MsgBox("Before ", MsgBoxStyle.Information, "SetupControls ")
Buttons[0] = Button1;
Buttons[1] = Button2;
//MsgBox("After ", MsgBoxStyle.Information, "SetupControls")
cmbAlgorithm.Items.Clear();
cmbAlgorithm.Items.Insert(0, "0 - esriHSVAlgorithm");
cmbAlgorithm.Items.Insert(1, "1 - esriCIELabAlgorithm");
cmbAlgorithm.Items.Insert(2, "2 - esriLabLChAlgorithm");
cmbAlgorithm.SelectedIndex = 0;
cmdOK.Enabled = false;
chkShowColors.CheckState = System.Windows.Forms.CheckState.Unchecked;
UpdateRamp();
chkShowColors_CheckStateChanged(chkShowColors, new System.EventArgs());
}
private void ShowColorsArray()
{
if (m_enumNewColors == null)
return;
else
{
//
// Iterate and show all colors in the ColorRamp.
//
HideColors();
LooRGBColors(); //m_lngColors
}
}
private void LooRGBColors()
{
//
// Move to the required Color to show. We only have space to show ten colors
// at a time on the form. So when we wish to show the next ten colors,
// (colors 11-20, 21-30 etc) we iterate the colors enumeration appropriately.
//
int lngMoveNext = 0;
m_enumNewColors.Reset();
while (lngMoveNext < m_lngColors)
{
m_enumNewColors.Next();
lngMoveNext = lngMoveNext + 1;
}
//
// Show colors in textboxes as necessary.
//
IColor colNew = null;
int lngCount = 0;
for (lngCount = 0; lngCount <= 9; lngCount++)
{
//commented the control array txtColor - OLD
//With txtColor(lngCount)
// colNew = m_enumNewColors.Next
// '
// ' If getting the next color returns nothing, we have got to
// ' the end of the colors enumeration.
// '
// If colNew Is Nothing Then
// Exit For
// End If
// .BackColor = System.Drawing.ColorTranslator.FromOle(colNew.RGB)
// .Visible = True
//End With
colNew = m_enumNewColors.Next();
//
// If getting the next color returns nothing, we have got to
// the end of the colors enumeration.
//
if (colNew == null)
break;
TextBoxColors[lngCount].BackColor = System.Drawing.ColorTranslator.FromOle(colNew.RGB);
TextBoxColors[lngCount].Visible = true;
//Commented the control array lblIndex - OLD
//With lblIndex(lngCount)
// .Text = CStr(lngCount + m_lngColors)
// .Visible = True
//End With
//LabelsIndex[lngCount].Text = System.Convert().ToString(lngCount + m_lngColors);
LabelsIndex[lngCount].Text = Convert.ToString (lngCount+m_lngColors);
LabelsIndex[lngCount].Visible = true;
}
}
private void HideColors()
{
//
// Hide all Color textboxes.
//
short i = 0;
for (i = 0; i <= 9; i++)
{
//txtColor(i).Visible = False '- OLD control array
TextBoxColors[i].Visible = false;
//lblIndex(i).Visible = False '- OLD control array
LabelsIndex[i].Visible = false;
}
}
private void frmAlgorithmicColorRamp_FormClosing(object eventSender, System.Windows.Forms.FormClosingEventArgs eventArgs)
{
bool Cancel = eventArgs.Cancel;
System.Windows.Forms.CloseReason UnloadMode = eventArgs.CloseReason;
if (m_enumNewColors == null)
{
//
// User has exited the Form without setting a new ColorRamp.
//
MessageBox.Show("You have not created a new ColorRamp." + System.Environment.NewLine + "Your symbology will not be changed.", "ColorRamp not set", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
eventArgs.Cancel = Cancel;
}
private void chkShowColors_Click(object sender, System.EventArgs e)
{
if (chkShowColors.CheckState == System.Windows.Forms.CheckState.Checked)
{
this.Width = (int)Support.TwipsToPixelsX(3705);
ShowColorsArray();
}
else
this.Width = (int)Support.TwipsToPixelsX(2355);
}
private void chkShowColors_CheckStateChanged(object eventSender, System.EventArgs eventArgs)
{
//
// Show and hide the colors array.
//
if (chkShowColors.CheckState == System.Windows.Forms.CheckState.Checked)
{
this.Width = (int)Support.TwipsToPixelsX(3705);
ShowColorsArray();
}
else
this.Width = (int)Support.TwipsToPixelsX(2355);
}
private void frmAlgorithmicColorRamp_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
IColorSelector ColorSelector = null;
if (Buttons[0] == sender)
{
// Create color selector object.
ColorSelector = new ColorSelectorClass();
// Open the selector dialog.
if (ColorSelector.DoModal(this.Handle.ToInt32()))
{
//
// A Color was selected (if the above method returned false,
// this indicates that the user pressed Cancel).
txtStartColor.BackColor = System.Drawing.ColorTranslator.FromOle(ColorSelector.Color.RGB);
}
UpdateRamp();
}
else
{
ColorSelector = new ColorSelectorClass();
if (ColorSelector.DoModal(this.Handle.ToInt32()))
txtEndColor.BackColor = System.Drawing.ColorTranslator.FromOle(ColorSelector.Color.RGB);
UpdateRamp();
}
}
private void Label9_Click(object sender, System.EventArgs e)
{
}
private void _txtColor_0_TextChanged(object sender, System.EventArgs e)
{
}
}
} //end of root namespace
[Visual Basic .NET]
frmAlgorithmicColorRamp.vb
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports System
Imports System.Collections
Imports System.Data
Imports System.Diagnostics
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.Compatibility
Friend Class frmAlgorithmicColorRamp
Inherits System.Windows.Forms.Form
' This form allows a user to set properties determining the constraints of a
' AlgoritmicColorRamp, which is then used to populate an existing ClassBreaksRenderer
' on an existing FeatureLayer.
'
'
' The m_lngClasses variable is set by the calling function, to indicate the
' number of random colors required by the classbreaksrenderer.
'
Public m_lngClasses As Integer
'
' The m_enumNewColors variable holds the colors to be returned to the calling function.
'
Public m_enumNewColors As IEnumColors
'
' The m_lngColors variable holds the index of the last color displayed in the array.
'
Private m_lngColors As Integer
Private Buttons(2) As System.Windows.Forms.Button
Private LabelsIndex(10) As System.Windows.Forms.Label
Private TextBoxColors(10) As System.Windows.Forms.TextBox
Private Sub cmbAlgorithm_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbAlgorithm.SelectedIndexChanged
If cmbAlgorithm.SelectedIndex > -1 Then
Call UpdateRamp()
End If
End Sub
Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
'
' User pressed Cancel, so we set the colors enumeration to nothing and
' unload the form.
'
m_enumNewColors = Nothing
Me.Close()
End Sub
Private Sub cmdEnumColorsNext_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEnumColorsNext.Click
'
' Increase the indicator variable m_lngColors by 10, so we can display the
' next ten colors to the user.
'
If Not m_enumNewColors Is Nothing Then
m_lngColors = m_lngColors + 10
Call HideColors()
Call LooRGBColors()
End If
End Sub
Private Sub cmdEnumColorsFirst_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEnumColorsFirst.Click
'
' Reset the indicator variable to zero.
'
If Not m_enumNewColors Is Nothing Then
m_lngColors = 0
Call HideColors()
Call LooRGBColors()
End If
End Sub
Private Sub cmdOK_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOK.Click
'
' Check we have a colors enumeration.
'
If m_enumNewColors Is Nothing Then
MsgBox("You have not created a new color ramp." & "Your layer symbology will be unchanged.", MsgBoxStyle.Information, "No Ramp Created")
Else
Me.Hide()
End If
End Sub
Private Sub frmAlgorithmicColorRamp_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
'
' Initialize the controls.
'
Call SetupControls()
End Sub
Private Sub UpdateRamp()
'
' Create a new AlgorithmicColorRamp object, and get it's
' IAlgorithmicColorRamp interface.
'
Dim AlgortihmicColorRamp As IAlgorithmicColorRamp
AlgortihmicColorRamp = New ESRI.ArcGIS.Display.AlgorithmicColorRamp
'
' Set the size of the color ramp ot the number of classes
' to be renderered.
'
AlgortihmicColorRamp.Size = m_lngClasses
'
' Set the color ramps properties.
'
Dim RGBColor As IRgbColor
With AlgortihmicColorRamp
RGBColor = New RgbColor
RGBColor.RGB = System.Drawing.ColorTranslator.ToOle(txtStartColor.BackColor)
.FromColor = RGBColor
RGBColor.RGB = System.Drawing.ColorTranslator.ToOle(txtEndColor.BackColor)
.ToColor = RGBColor
.Algorithm = cmbAlgorithm.SelectedIndex
End With
Dim boolRamp As Boolean
If AlgortihmicColorRamp.Size > 0 Then
boolRamp = True
AlgortihmicColorRamp.CreateRamp(boolRamp)
If boolRamp Then
m_enumNewColors = AlgortihmicColorRamp.Colors
m_enumNewColors.Reset()
cmdOK.Enabled = True
'
' Check if we should be showing the colors.
'
If chkShowColors.CheckState = System.Windows.Forms.CheckState.Checked Then
'
' Populate the Colors textbox array and it's labels.
'
m_lngColors = 0
Call ShowColorsArray()
End If
End If
End If
End Sub
Private Sub SetupControls()
LabelsIndex(0) = Label1
LabelsIndex(1) = Label2
LabelsIndex(2) = Label3
LabelsIndex(3) = Label4
LabelsIndex(4) = Label5
LabelsIndex(5) = Label6
LabelsIndex(6) = Label7
LabelsIndex(7) = Label8
LabelsIndex(8) = Label9
LabelsIndex(9) = Label10
TextBoxColors(0) = TextBox1
TextBoxColors(1) = TextBox2
TextBoxColors(2) = TextBox3
TextBoxColors(3) = TextBox4
TextBoxColors(4) = TextBox5
TextBoxColors(5) = TextBox6
TextBoxColors(6) = TextBox7
TextBoxColors(7) = TextBox8
TextBoxColors(8) = TextBox9
TextBoxColors(9) = TextBox10
Call HideColors()
txtStartColor.Text = ""
txtEndColor.Text = ""
txtStartColor.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFS)
txtEndColor.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFS)
'MsgBox("Before ", MsgBoxStyle.Information, "SetupControls ")
Buttons(0) = Button1
Buttons(1) = Button2
'MsgBox("After ", MsgBoxStyle.Information, "SetupControls")
cmbAlgorithm.Items.Clear()
cmbAlgorithm.Items.Insert(0, "0 - esriHSVAlgorithm")
cmbAlgorithm.Items.Insert(1, "1 - esriCIELabAlgorithm")
cmbAlgorithm.Items.Insert(2, "2 - esriLabLChAlgorithm")
cmbAlgorithm.SelectedIndex = 0
cmdOK.Enabled = False
chkShowColors.CheckState = System.Windows.Forms.CheckState.Unchecked
Call UpdateRamp()
Call chkShowColors_CheckStateChanged(chkShowColors, New System.EventArgs())
End Sub
Private Sub ShowColorsArray()
If m_enumNewColors Is Nothing Then
Exit Sub
Else
'
' Iterate and show all colors in the ColorRamp.
'
Call HideColors()
LooRGBColors() 'm_lngColors
End If
End Sub
Private Sub LooRGBColors()
'
' Move to the required Color to show. We only have space to show ten colors
' at a time on the form. So when we wish to show the next ten colors,
' (colors 11-20, 21-30 etc) we iterate the colors enumeration appropriately.
'
Dim lngMoveNext As Integer
m_enumNewColors.Reset()
Do While lngMoveNext < m_lngColors
m_enumNewColors.Next()
lngMoveNext = lngMoveNext + 1
Loop
'
' Show colors in textboxes as necessary.
'
Dim colNew As IColor
Dim lngCount As Integer
For lngCount = 0 To 9
'commented the control array txtColor - OLD
'With txtColor(lngCount)
' colNew = m_enumNewColors.Next
' '
' ' If getting the next color returns nothing, we have got to
' ' the end of the colors enumeration.
' '
' If colNew Is Nothing Then
' Exit For
' End If
' .BackColor = System.Drawing.ColorTranslator.FromOle(colNew.RGB)
' .Visible = True
'End With
With TextBoxColors(lngCount)
colNew = m_enumNewColors.Next
'
' If getting the next color returns nothing, we have got to
' the end of the colors enumeration.
'
If colNew Is Nothing Then
Exit For
End If
.BackColor = System.Drawing.ColorTranslator.FromOle(colNew.RGB)
.Visible = True
End With
'Commented the control array lblIndex - OLD
'With lblIndex(lngCount)
' .Text = CStr(lngCount + m_lngColors)
' .Visible = True
'End With
LabelsIndex(lngCount).Text = CStr(lngCount + m_lngColors)
LabelsIndex(lngCount).Visible = True
Next lngCount
End Sub
Private Sub HideColors()
'
' Hide all Color textboxes.
'
Dim i As Short
For i = 0 To 9
'txtColor(i).Visible = False '- OLD control array
TextBoxColors(i).Visible = False
'lblIndex(i).Visible = False '- OLD control array
LabelsIndex(i).Visible = False
Next i
End Sub
Private Sub frmAlgorithmicColorRamp_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim Cancel As Boolean = eventArgs.Cancel
Dim UnloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason
If m_enumNewColors Is Nothing Then
'
' User has exited the Form without setting a new ColorRamp.
'
MsgBox("You have not created a new ColorRamp." & vbNewLine & "Your symbology will not be changed.", MsgBoxStyle.Information, "ColorRamp not set")
End If
eventArgs.Cancel = Cancel
End Sub
Private Sub chkShowColors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkShowColors.Click
If chkShowColors.CheckState = System.Windows.Forms.CheckState.Checked Then
Me.Width = VB6.TwipsToPixelsX(3705)
Call ShowColorsArray()
Else
Me.Width = VB6.TwipsToPixelsX(2355)
End If
End Sub
Private Sub chkShowColors_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkShowColors.CheckStateChanged
'
' Show and hide the colors array.
'
If chkShowColors.CheckState = System.Windows.Forms.CheckState.Checked Then
Me.Width = Microsoft.VisualBasic.Compatibility.VB6.TwipsToPixelsX(3705)
Call ShowColorsArray()
Else
Me.Width = VB6.TwipsToPixelsX(2355)
End If
End Sub
Private Sub frmAlgorithmicColorRamp_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
Dim ColorSelector As IColorSelector
If Buttons(0) Is sender Then
' Create color selector object.
ColorSelector = New ColorSelector
' Open the selector dialog.
If ColorSelector.DoModal(Me.Handle.ToInt32) Then
'
' A Color was selected (if the above method returned false,
' this indicates that the user pressed Cancel).
txtStartColor.BackColor = System.Drawing.ColorTranslator.FromOle(ColorSelector.Color.RGB)
End If
Call UpdateRamp()
Else
ColorSelector = New ColorSelector
If ColorSelector.DoModal(Me.Handle.ToInt32) Then
txtEndColor.BackColor = System.Drawing.ColorTranslator.FromOle(ColorSelector.Color.RGB)
End If
Call UpdateRamp()
End If
End Sub
Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
End Sub
Private Sub _txtColor_0_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class