Color Wells

Overview

A color well is a button that shows a color. You find them in drawing apps, slide editors and website builders, anywhere a person picks the color of a shape, a background or some text. Pressing the well opens a color picker, where a person can choose a new color with a color wheel, sliders, a hex code or a row of ready-made colors. In this tutorial, you will add color wells to a form, read the color a person chose, check whether two colors are easy to read together, and make your program react the moment a color changes.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Add a color well to a form with AddColorWell and give it a clear name
  • Read the chosen color with GetColor and GetColorText
  • Use a ColorWellListener to react when a person picks a new color
  • Check whether text will be easy to read on a background with CalculateContrast
  • Explain how a person using a keyboard or a screen reader picks a color

Warm up

Think of a poster or a slide that was hard to read. What made it hard? Was it the size of the text, or the colors? Now think about how you would tell someone a color without showing it to them. Would you say "blue"? Which blue?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
Color wellA button that shows a color. Pressing it opens a color picker to choose a new color.
Color pickerThe panel a color well opens. It has a hue ring, a saturation and brightness square, red, green and blue sliders, a hex field, 16 swatches, and Cancel and OK buttons.
Hex codeA color written as a pound sign and six hex digits, such as #2F5BBB. The first two digits are the red, the next two the green and the last two the blue, each from 00 to FF.
HueWhere a color sits around the color wheel, from 0 to 360 degrees. Red is at 0, green at 120 and blue at 240.
SwatchA small square of one ready-made color that a person can pick with one click or key.
Contrast ratioA number from 1 to 21 that says how different two colors look in lightness. Text is easier to read when the contrast with its background is higher.
ListenerAn object that a control tells when something happens. A ColorWellListener is told when a person changes a color well's color.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
AddColorWell(text name, Color color)ColorWell fill = page:AddColorWell("Fill color", blue)Adds a color well to a form page that starts with the color given. The name is what a screen reader says.
AddColorWell(text name)ColorWell fill = page:AddColorWell("Fill color")Adds a color well that starts black.
GetColor()Color chosen = fill:GetColor()Returns a copy of the well's color.
GetColorText()text words = fill:GetColorText()Returns the color as words: its hex code, then the name of the closest named color, such as "#4A5A6E, dim gray".
GetColorWell(text name)ColorWell fill = page:GetColorWell("Fill color")Finds a color well on a page by its name.
SetColor(Color color)fill:SetColor(palette:Teal())Changes the well's color from code. Listeners are not told.
SetHexColor(text hex)blue:SetHexColor("#2F5BBB")Sets a Color from a hex code.
CalculateContrast(Color other)number ratio = background:CalculateContrast(lettering)Returns the contrast ratio between two colors, from 1 to 21.
AddColorWellListener(ColorWellListener listener)fill:AddColorWellListener(listener)Tells the listener each time a person changes the color.
ColorWellChanged(ColorWellChangedEvent event)action ColorWellChanged(ColorWellChangedEvent event)The action a listener writes. The event's GetColorWell, GetOldColor, GetNewColor and IsFinal say what changed.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-ALG-IM-09

High School · Algorithms & Design

Design a computing technology using human-centered design principles.

Also in 7 other lessons

MS-ALG-IM-09

Middle School · Algorithms & Design

Evaluate which human-centered design principles are present or missing in an existing computing technology.

Also in 5 other lessons

S1-SWD-UX-04

Specialty I · Software Development

Refine a user interface design using accessibility and responsive design tools.

Also in 13 other lessons

HS-ALG-PS-03

High School · Algorithms & Design

Evaluate algorithms for efficiency, correctness, and clarity, using metrics or test cases.

Also in 4 other lessons

Every standard, and the lessons that cover it

Explore

What is a color well?

A computer stores a color as three numbers: how much red, how much green and how much blue light to mix. Quorum's Color class keeps each of these as a number from 0 to 1. People rarely think about colors that way, though, so programs let them pick colors directly instead, with the mouse or the keyboard. A color well is the control that does this. It is a kind of button: it shows a small square of its color and the color's hex code, such as #2F5BBB. Pressing it opens a color picker just below it.

The color picker has several ways to choose the same thing, so a person can use whichever is easiest for them:

  • a hue ring, a circle of colors around the edge that picks the hue;
  • a square inside the ring that picks the saturation (from gray at the left to full color at the right) and the brightness (from black at the bottom to bright at the top);
  • Red, Green and Blue slider fields, each from 0 to 255, like the ones in the Sliders tutorial;
  • a Hex field to type a hex code into;
  • 16 swatches, the basic colors of HTML and CSS, such as black, red, navy and teal;
  • Cancel and OK buttons.

Changing any part changes the others, and the well shows the new color as it changes. OK keeps the new color. Cancel puts the old one back.

Making color wells on a form

The easiest way to make a color well is on a form, which you met in the Forms tutorial. A page's AddColorWell action makes a color well, adds it to the page and returns it. The first value is the well's name. A screen reader says the name, so it should say what the color is for, such as "Background color." The second value is the color it starts with. If you leave the color out, the well starts black.

A color well shows its hex code, not its name. That means a person who can see the screen needs a label to know what each well is for. Add a label with AddLabel just before each well.

The program below makes a poster designer with two wells, one for the background and one for the text, and a button that checks the colors.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.ColorWell
use Libraries.Interface.Controls.Button
use Libraries.Game.Graphics.Color

Form form
Page page = form:GetMainPage()
page:SetTitle("Poster Colors")

Color palette

page:AddLabel("Background label", "Background color")
ColorWell background = page:AddColorWell("Background color", palette:Navy())

page:AddLabel("Text label", "Text color")
ColorWell lettering = page:AddColorWell("Text color", palette:White())

Button checkButton = page:AddButton("Check colors")
checkButton:SetDescription("Shows the colors you picked and whether the text is easy to read.")

CheckColorsBehavior behavior
checkButton:SetBehavior(behavior)

form:Display()

palette:Navy() and palette:White() are two of the colors the Color class has ready-made. You can also make any color from its hex code with SetHexColor:

Color blue
blue:SetHexColor("#2F5BBB")
ColorWell jersey = page:AddColorWell("Jersey color", blue)

Reading the chosen color

A color well has two ways to tell you its color:

  • GetColor returns a copy of the color as a Color object, which you can use for drawing or for math;
  • GetColorText returns the color as words: its hex code, then the name of the closest named color. Navy is "#000080, navy," and #4A5A6E is "#4A5A6E, dim gray."

The name comes after the hex code because it is only the closest of a list of about 140 named colors, so it is a good hint, not an exact answer.

The Color class can also measure how readable two colors are together. Its CalculateContrast action returns the contrast ratio between two colors, a number from 1 (the same lightness) to 21 (black on white). The Web Content Accessibility Guidelines, a standard from the W3C, ask for a ratio of at least 4.5 to 1 for normal-size text, in success criterion 1.4.3, "Contrast (Minimum)" (WCAG 2.1).

Make a class in a file named CheckColorsBehavior.quorum. It comes from FormBehavior, which gives it the GetPage action, so it can find each color well by its name with GetColorWell.

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.ColorWell
use Libraries.Game.Graphics.Color

class CheckColorsBehavior is FormBehavior
    action Run(BehaviorEvent event)
        Page page = GetPage()
        ColorWell backgroundWell = page:GetColorWell("Background color")
        ColorWell textWell = page:GetColorWell("Text color")
        if backgroundWell = undefined
            return now
        end
        if textWell = undefined
            return now
        end

        Color back = backgroundWell:GetColor()
        Color front = textWell:GetColor()
        number contrast = back:CalculateContrast(front)

        // Round to one decimal place, such as 8.6.
        integer tenths = cast(integer, contrast * 10 + 0.5)
        number rounded = tenths / 10.0

        output "Background: " + backgroundWell:GetColorText()
        output "Text: " + textWell:GetColorText()
        output "Contrast: " + rounded + " to 1"
        if contrast >= 4.5
            output "Text on this background is easy to read."
        else
            output "Text on this background is hard to read. Try colors that are farther apart in lightness."
        end
    end
end

Run the program and press "Check colors." Navy and white are far apart, so the contrast is high. Now open the text well, pick the gray swatch, press OK and check again. Gray on navy is much harder to read.

Choosing a color with the keyboard

Everything in the picker works from the keyboard:

The color picker's keys
KeyWhat it does
Space or Enter on the wellOpens the picker, with the focus on the hue ring
Tab, Shift+TabMoves through the ring, the square, the red, green and blue sliders and their number fields, the hex field, the swatches, Cancel and OK
Left, Right (or Up, Down) on the ringTurns the hue 10 degrees; with Shift, 1 degree
Page Up, Page Down on the ringTurns the hue 30 degrees
Left, Right on the squareLowers or raises the saturation 10 percent; with Shift, 1 percent
Up, Down on the squareRaises or lowers the brightness 10 percent; with Shift, 1 percent
Arrow keys on the swatchesMoves to the next or previous swatch and picks it
EnterKeeps the new color and closes the picker, like OK
EscapePuts the old color back and closes the picker, like Cancel

However the picker closes, the focus goes back to the well. A click outside the picker cancels. You can use Escape for the same purpose.

Tabbing into the swatches doesn't pick one. A person can tab past them without changing the color, which is different from the radio buttons on a form.

Reacting when the color changes

Sometimes a program should react the moment a color changes, not when another button is pressed. A color well tells listeners about each change. A listener is a class that comes from ColorWellListener and writes one action, ColorWellChanged.

A color well sends two kinds of changes:

  • while the picker is open, each change a person makes, such as each step of the hue ring, is sent as a change that is not final;
  • when the picker closes, one more change is sent as final: after OK if the color changed, or after Cancel if it has to put the old color back.

The event's IsFinal action tells you which kind you have. Use the changes that are not final for quick things, such as a preview, and the final change for anything that should happen once.

Make a class in a file named ColorReportListener.quorum. It shows each final change in a label on the page:

use Libraries.Interface.Events.ColorWellListener
use Libraries.Interface.Events.ColorWellChangedEvent
use Libraries.Interface.Controls.ColorWell
use Libraries.Game.Graphics.Label

class ColorReportListener is ColorWellListener
    Label report = undefined

    action SetReport(Label label)
        report = label
    end

    action ColorWellChanged(ColorWellChangedEvent event)
        if event:IsFinal()
            ColorWell well = event:GetColorWell()
            report:SetText(well:GetName() + " is now " + well:GetColorText() + ".")
        end
    end
end

Now give the listener to a color well:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.ColorWell
use Libraries.Game.Graphics.Color
use Libraries.Game.Graphics.Label

Form form
Page page = form:GetMainPage()
page:SetTitle("Team Colors")

Color blue
blue:SetHexColor("#2F5BBB")
page:AddLabel("Jersey label", "Jersey color")
ColorWell jersey = page:AddColorWell("Jersey color", blue)

Label report = page:AddLabel("Report", "No changes yet.")

ColorReportListener listener
listener:SetReport(report)
jersey:AddColorWellListener(listener)

form:Display()

Open the well, move the hue ring a few steps and press OK. The label changes once, when the picker closes. Try it again and press Cancel instead. The label shows the old color, because the cancel put it back.

A program's own SetColor doesn't tell listeners, because a person didn't make the change. If you add jersey:SetColor(blue) to the program, the label still says "No changes yet."

Blocks

What a screen reader says

A color can't be heard, so a color well gives screen readers words for it. The well's name is the name you gave it, such as "Jersey color." Its description is its color text: the hex code, then the closest name, such as "#2F5BBB, royal blue." The well keeps the description up to date as the color changes, so you don't set it yourself. When the well gets the focus, a screen reader reads its name and its color. The exact words depend on the screen reader.

Inside the picker, each part says what it is:

  • the picker is a group with the well's name, such as "Jersey color";
  • the hue ring is a slider that says its degrees and the name of the color at that hue;
  • the square has two values, saturation and brightness, each in percent with the name of the color;
  • each swatch says its hex code and name, such as "#FF0000, red."

Nothing extra is spoken when the color changes. A person who wants to check the color moves the focus to the well, or to the report label, and the screen reader reads it.

Colors of the well itself

The well's own background, border and focus border come from the current theme, so they change in dark mode too. The Themes and Dark Mode tutorial shows how to change them with the theme key "ColorWell."

Engage

In this activity, you build a "Make a Sign" form for a school event, then test and evaluate it yourself. The form needs at least three color wells, with labels: one for the background, one for the title text and one for the small text. Give each well a clear name. Add a "Check sign" button. When it is pressed, output the contrast ratio between the background and each text color, and output a warning for any pair below 4.5 to 1.

Then add a listener to the background well that reports each final change in a label, so that a screen reader reads the new color when a person moves to the label.

When it works, test your form as if you had never used it. Use only the keyboard: Tab, Space, the arrow keys, Enter and Escape. If you have a screen reader, turn it on and notice what it reports for each well. If not, examine the name of the color. Then evaluate your design with this self-check:

  • Does every well have a name that says what it colors?
  • After a change, does the person get feedback, such as the label?
  • Can a person back out of a mistake, such as choosing the wrong color?
  • Does "Check sign" warn about every pair below 4.5 to 1?

For each "no", write which human-centered design idea is missing. Fix at least one of them, and test again.

Wrap up

A color well is a button that shows a color and opens a picker to change it. You can read the color with GetColor and GetColorText, or react right away with a ColorWellListener, using IsFinal to tell a preview from a finished choice. Because a color can't be heard, the well says it in words: the hex code first, then the closest name. Think back to the warm up: how would you now tell someone a color without showing it? In the next tutorial, Calendars and Dates, you will let a person pick a day.