Unit 3
custom colors
 

Up to now we have been limited to the 13 basic colors of java. 
There are SO many more RGB colors available.

RGB stands for   (red green blue). 

Every color on the computer is made by some red, some green and some blue with levels from 0 - 255
(in hexadecimals this is 0 - FF )- web pages use the same colors but as hexadecimals. Java uses the decimal version of each number.

For example, red is (255, 0, 0)  , green is (0, 255, 0), and blue is (0, 0, 255). 

Black is  (0, 0, 0), and  white is (255, 255, 255). 

Lower numbers produce darker colors, whereas higher numbers produce brighter colors.


In theory, there are 256 * 256* 256 colors ( or about 16.7 million colors) but our eyes and these monitors can't really see the difference between most of them.

Many colors have HTML names attached to them. Here are some of those.

The numbers under each name are needed for us to get that particular color. Continue below the color swatches

aliceblue
240,248,255

darkslategray
47,79,79

lightsalmon
255,160,122

paleviolet
219,112,147

papayawhip
255,239,213

antiquewhite
250,235,215

darkturquoise
0,206,209

lightseagreen
32,178,170

pink
255,192,203

plum
221,160,221

wheat
245,222,179

darkviolet
148,0,211

lightskyblue
135,206,250

peachpuff
255,239,213

powderblue
176,224,230

aquamarine
127,255,212

deeppink
255,20,147

lightslategray
119,136,153

peru
205,133,63

saddlebrown
139,69,19

azure
240,255,255

deepskyblue
0,191,255

lightsteelblue
176,196,222

purple
128,0,128

rosybrown
188,143,143

beige
245,245,220

dimgray
105,105,105

lightyellow
255,255,224

royalblue
65,105,225

salmon
250,128,114

bisque
255,228,196

dodgerblue
30,144,255

lightgreen
144,238,144

sandybrown
244,164,96

seagreen
46,139,87

darksalmon
233,150,122

firebrick
178,34,34

limegreen
50,205,50

silver
192,192,192

yellowgreen
154,205,50

blanchedalmond
255,255,205

mediumaquamarine
102,205,170

linen
250,240,230

lightgoldenrodyellow
250,250,210

paleturquoise
175,238,238

orchid
218,112,214

forestgreen
34,139,34

palegreen
152,251,152

lightpink
255,182,193

darkslateblue
72,61,139

blueviolet
138,43,226

darkseagreen
143,188,143

maroon
128,0,0

burlywood
222,184,135

brown
165,42,42

cadetblue
95,158,160

gold
255,215,0

mediumorchid
186,85,211

chartreuse
127,255,0

mediumblue
0,0,205

chocolate
210,105,30

gray
128,128,128

mediumpurple
147,112,219

seashell
255,245,238

goldenrod
218,165,32

coral
255,127,80

green
0,128,0

mediumslateblue
123,104,238

sienna
160,82,45

mediumseagreen
60,179,113

cornflowerblue
100,149,237

greenyellow
173,255,47

palegoldenrod
238,232,170

indianred
205,92,92

midnightblue
25,25,112

crimson
220,20,60

hotpink
255,105,180

mediumvioletred
199,21,133

slateblue
106,90,205

slategray
112,128,144

darkcyan
0,139,139

ivory
255,240,240

mistyrose
255,228,225

springgreen
0,255,127

darkblue
0,0,139

darkgoldenrod
184,134,11

khaki
240,230,140

moccasin
255,228,181

steelblue
70,130,180

mediumspringgreen
0,250,154

darkgray
169,169,169

lavender
230,230,250

mediumturquoise
72,209,204

skyblue
135,206,235

indigo
75,0,130

darkgreen
0,100,0

tan
210,180,140

navy
0,0,128

teal
0,128,128

navajowhite
255,222,173

darkmagenta
139,0,139

thistle
216,191,216

olive
128,128,0

tomato
253,99,71

darkkhaki
189,183,107

darkolivegreen
85,107,47

lighblue
173,216,230

olivedrab
107,142,35

turquoise
64,224,208

lawngreen
124,252,0

darkorange
255,140,0

lightcoral
240,128,128

orange
255,165,0

violet
238,130,238

darkred
139,0,0

darkorchid
153,50,204

lightcyan
224,255,255

orangered
255,69,0

wheat
245,222,179

lightgrey
211,211,211

 

There are two ways that you can use these colors.

Copy and run this program.

After you have see it. Unblock the /* */ to see how to get shades. We can go brighter and darker easily, but this does not work well with the built-in colors. They may already be as dark or as bright as possible.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 

public class Lab3colors extends JFrame
{
//variables

JButton b1, b2,b3,b4,b5,b6;
JLabel L1, L2, L3;


public Lab3colors ()
{
  super("colors");
  Container container = getContentPane();
  container.setLayout(new GridLayout(12,1));
  setSize(400,300); //Set Size of Window

// create objects
  L1 = new JLabel(" Here are 3 samples of new colors ");

  b1 = new JButton("coral ");
  b1.setBackground(new Color ( 255,127,80));

  b2= new JButton(" steelblue");
  b2.setBackground(new Color ( 70,130,180));

  b3 = new JButton("indigo");
  b3.setForeground(Color.white);
  b3.setBackground(new Color ( 75,0,130));

//add to the window container
 
container.add(L1);
   container.add(b1);
   container.add(b2);

   container.add(b3);



/*
   L2 = new JLabel("a color with a brighter and darker version");

   b4 = new JButton("brighter mauve ");
   b4.setBackground(new Color ( 128, 100, 128).brighter());

   b5= new JButton("mauve");
   b5.setBackground(new Color ( 128, 100, 128));

   b6 = new JButton("darker mauve ");
       b6.setBackground(new Color ( 128, 100, 128).darker());

   
container.add(L2);
   container.add(b4);
   container.add(b5);
   container.add(b6);


*/


setVisible(true);//Make Window Visible
}

public static void main(String args[])
{
  Lab3colors prog = new Lab3colors ();
  prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

There are times when we are using a new color many times. This an optional way to use custom colors

It is useful to declare that color as a constant. Add these parts in the appropriate area and try it.

1.  Add this to the variable declaration section

JButton b10, b11, b12;
Color ORCHID = new Color(218,112,214);
Color SKYBLUE = new Color(135,206,235);
Color OLIVE = new Color(128,128,0);

2. Add these after the unblocked section

L3 = new JLabel("Colors as constants");

b10= new JButton("Orchid");
b10.setBackground(ORCHID);

b11= new JButton("sky blue");
b11.setBackground(SKYBLUE);

b12= new JButton("olive");
b12.setBackground(OLIVE);

container.add(L3);
container.add(b10);
container.add(b11);
container.add(b12);

 

This sliding rule will give you an idea of exactly how many colors are available. To use a color, you will need to copy the numbers into your program

 

Assignment
It is not necessary to change any of the Lab names.
If you want to, add the word new to the title: example Lab3a2 becomes Lab3a2new


Open Lab3a2 ( red, white, and blue) add setFonts to the 3 JLabel- pick 3 different fonts - increase window size if you need to - all JLabels must be on the same line. Here is my sample. Use custom colors if you want to.

 

Open Lab3a3,
      1. Add a custom color to the background
      2. Change the foreground of the JLabels to 2 different custom colors.
      3. Make all the JLabels a bold font of your choice.
Increase window size if you need to.

Open Lab3b3 - remember we are using this again - make it "nice"
    1. Add a custom colors to JLabels and or JTextfields.
    2. Change title JLabels text to a bold font,
    3. Change other 4 JLabels text to the same font not bold, and maybe one size smaller
    4. Change the JTextFields to one font.
 Increase window size by a little if you need to.