-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rainbow.java
executable file
·85 lines (68 loc) · 2.4 KB
/
Rainbow.java
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
/*
* Creates a range of colors for the Mandelbrot Set using a singleton class
*
* @author Harry Pinkerton
* @version 1.0
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.util.*;
import java.awt.Graphics2D;
public class Rainbow extends JComponent{
private static Rainbow instance;
Color[] colors;
Color[] seeds = {Color.RED, Color.ORANGE, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLUE, Color.MAGENTA};;
int limit;
int index;
// Instance Variables
public void setLimit(int limit){
this.limit = limit;
System.out.print("Rainbow " + limit);
}
public void setGradient(String gradient){
if (gradient.equals("Rainbow")){
seeds = new Color[] {Color.RED, Color.ORANGE, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLUE,
Color.MAGENTA};
}
if (gradient.equals("GreyScale")){
seeds = new Color[] {Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.BLACK};
}
if (gradient.equals("BlueScale")){
seeds = new Color[] {Color.BLUE, Color.CYAN, Color.LIGHT_GRAY};
}
}
public static Rainbow getInstance(int n){
if (instance == null){
instance = new Rainbow(n);
}
return instance;
}
public void createGradient(int limit){
this.limit = limit;
colors = new Color[limit];
double numSeeds = seeds.length;
double numSpaces = (int)((limit - 1)/(numSeeds -1));
for (int i =0; i < limit; i++){
Color startColor = seeds[(int)(i / numSpaces)];
Color endColor = seeds[Math.min((int)(i / numSpaces) + 1, (seeds.length -1))];
double percent = (i % ((int) numSpaces)) / (numSpaces);
int r = (int) (percent * (endColor.getRed() - startColor.getRed()) + startColor.getRed());
int b = (int) (percent * (endColor.getBlue() - startColor.getBlue()) + startColor.getBlue());
int g = (int) (percent * (endColor.getGreen() - startColor.getGreen()) + startColor.getGreen());
colors[i] = new Color(r, g, b);
}
}
public Color getColor(int index){
if (index == limit){
return Color.BLACK;
}
else{
return colors[index];
}
}
private Rainbow(int n){
createGradient(n);
}
}