There are many possible approaches for such a color display. The simplest of them is given in the program below.

The core of this fragment is the initColorMap method. Interpolation requires several interpolation steps and an array of colors. In the screenshot it was
- red
- Red Green
- red, green, blue (as in the first image of the question)
- red, yellow, green, cyan, blue, magenta.
- black, orange, white, blue, blue (attempt to get a color map similar to the one indicated in the second image in the question)
- red, green, blue, with sine function
The method returns an int array containing the RGB values โโof the interpolated colors. This can be used directly. But for improved versatility, these arrays are wrapped in the ColorMap1D interface, which offers a method that returns an RGB color for any given value between 0.0 and 1.0.
In the case of your application, this can probably be used as follows:
double value = (double)iterations / maxIterations; int rgb = colorMap.getColor(value);
( EDIT : the following description and code has been updated and expanded based on a request in a comment)
Such "normalization" to the range [0.0, 1.0] and abstraction using interfaces are often useful.
As a demonstration of the effects that are possible with this abstraction: the ColorMaps1D class contains several methods for creating instances of ColorMap1D :
ColorMaps1D#createDefault(int steps, Color ... colors) : Creates a default color map that interpolates according to a given sequence of colors with a predetermined number of steps ("resolution" of the color map)ColorMaps1D#create(ColorMap1D delegate, DoubleFunction<Double> function) : this method creates a color map where the argument of the getColor method is converted with the given function before the getColor method of this delegate is passed.
Thus, you can easily create ColorMap1D , which interpolates non-linearly between colors. You can even create an implementation of ColorMap1D that will interpolate several other color maps.
As an example, I added a color map that uses the standard, simple color map Red-> Green-> Blue, but accesses it using a function that calculates the sine of an argument. Thus, you can cycle through the Red โ Green โ Blue map several times.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.util.Arrays; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ColorMapsTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new GridLayout(0,1)); int steps = 1024; f.getContentPane().add( createPanel(steps, Color.RED)); f.getContentPane().add( createPanel(steps, Color.RED, Color.GREEN)); f.getContentPane().add( createPanel(steps, Color.RED, Color.GREEN, Color.BLUE)); f.getContentPane().add( createPanel(steps, Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA)); f.getContentPane().add( createPanel(steps, Color.BLACK, Color.ORANGE, Color.WHITE, Color.BLUE, new Color(0,0,128))); JPanel panel = new JPanel(new BorderLayout()); Color colors[] = new Color[]{ Color.RED, Color.GREEN, Color.BLUE }; String info = "With sine over "+createString(colors); panel.add(new JLabel(info), BorderLayout.NORTH); ColorMapPanel1D colorMapPanel = new ColorMapPanel1D( ColorMaps1D.createSine( ColorMaps1D.createDefault(steps, colors), Math.PI * 4)); panel.add(colorMapPanel, BorderLayout.CENTER); f.getContentPane().add(panel); f.setSize(500, 400); f.setLocationRelativeTo(null); f.setVisible(true); } private static JPanel createPanel(int steps, Color ... colors) { JPanel panel = new JPanel(new BorderLayout()); String info = "In "+steps+" steps over "+createString(colors); panel.add(new JLabel(info), BorderLayout.NORTH); ColorMapPanel1D colorMapPanel = new ColorMapPanel1D(ColorMaps1D.createDefault(steps, colors)); panel.add(colorMapPanel, BorderLayout.CENTER); return panel; } private static String createString(Color ... colors) { StringBuilder sb = new StringBuilder(); for (int i=0; i<colors.length; i++) { sb.append(createString(colors[i])); if (i < colors.length - 1) { sb.append(", "); } } return sb.toString(); } private static String createString(Color color) { return "("+color.getRed()+","+color.getGreen()+","+color.getBlue()+")"; } }
(As for color smoothing: this is something that probably needs to be asked in a separate question. Or maybe not, because there are already a lot of questions in StackOverflow. For example, see the Smooth Spectrum to create a Mandelbrot (or many others))