Back

Activity 2.15: Raylib Lab 10 — Color as Data

In classBuilt in class, straight after Activity 2.14's deck.

divider

What You're Building

A palette of five monster colors, each drawn as a swatch with its three color numbers charted underneath. The swatch and the bars are the same information — one you read with your eyes, one you read as numbers. Then you take bits away and watch the colors fall apart.

Five color swatches labeled slime, lava, ice, swamp and ghost, each with a red, a green and a blue bar underneath showing its three channel values

Task 2: every color, and its numbers

divider

Activity 2.15

Raylib Lab 10 — Color as Data

Key Concepts

A Color Is Numbers

A Palette Is a Dataset

Bit Depth

Every Raylib Color So Far Had a Name

Color.SkyBlue, Color.Lime, Color.DarkBlue: every lab this year picked a color by name. That was abstraction, from Activity 2.14 — a name that let you ignore what was underneath.

Underneath, every one of them is four numbers. Today you make colors from the numbers directly, and then treat a whole palette as data.

A Color Is Four Numbers

new Color builds a color from four numbers: how much red, green and blue light to mix, and how solid it is.

Demo
Color slime = new Color(120, 220, 80, 255);

A Color Is Four Numbers

Value Trace
red 120 one byte: 0 to 255
green 220 one byte: 0 to 255
blue 80 one byte: 0 to 255
alpha 255 how solid: 255 is fully solid

Each channel is one byte — 2.14's eight bits, so 0 to 255. Three channels give 256 × 256 × 256 = 16,777,216 colors. Lots of green and some red, very little blue: a bright yellow-green.

A Palette Is a Dataset

Five colors are fifteen numbers. Put each channel in its own array, lined up by position, and the palette is three parallel arrays — the same shape as the race results and the leaderboards.

Demo
int[] reds = { 120, 230, 150, 90, 200 };
int[] greens = { 220, 90, 210, 110, 190 };
int[] blues = { 80, 40, 245, 60, 230 };

A Palette Is a Dataset

Anything you can put in an array you can chart. Predict: which of the five has the most blue?

The five swatches with red, green and blue bars under each

The third, ice, at 245. The bars answer that in a glance; the swatches alone would leave you guessing.

Bit Depth Is How Many Levels Each Channel Gets

Eight bits per channel gives 256 levels. Use only two bits and each channel gets 4 levels: 0, 64, 128, 192. To squeeze a value into those levels, round it down to the nearest step — 2.14's % again.

Value Trace
step = 256 / 4 = 64
120 - 120 % 64 = 120 - 56 = 64
220 - 220 % 64 = 220 - 28 = 192
80 - 80 % 64 = 80 - 16 = 64

Bit Depth Is How Many Levels Each Channel Gets

Predict which monster survives 4 levels best, and which falls apart completely at 2.

Three rows of the five swatches: full color, 4 levels per channel, and 2 levels per channel, where swamp is black and ice and ghost are the same gray

At 2 levels, swamp turns black, and ice and ghost become the same gray: two different colors, now identical. Activity 1.2 showed this with a slider; this time you built it.

Build It Now, Then One Last Check

You build it now, on the classroom computers — Raylib does not run in Juicemind.

Before the end of the session you will be asked to change the bit depth and predict the result before you run it. A student who built the lab can answer in seconds.

Today's Objectives

  • Making a color from its red, green, blue and alpha numbers
  • Treating a palette as parallel arrays and charting it
  • Reducing bit depth and calculating how many colors remain

Key Terms

Channel
One of the numbers that make a color: red, green or blue.
Alpha
How solid a color is, from 0 (invisible) to 255 (fully solid).
Bit depth
How many bits each channel gets, which sets how many levels it can have.

'F' → Fullscreen

divider

Build

Create a new Raylib program named 2-15-color-data.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.

This lab runs on the classroom computers, not in Juicemind. Close the window to end the program. Escape works too.


Task 1: The Swatches

  • Four parallel arrays: the monsters' names and their reds, greens and blues. DrawSwatch builds each color with new Color and stores it in a variable of type Color, just as an int variable stores a number. The background is a color made the same way.
  • Write the code below and run it. Then change one number in greens and run it again. Only one swatch should change. Put it back.
  • Add a sixth monster of your own. Write down how many arrays you had to change. It will not fit on screen until you change one number in the loop: find which. Then take your monster out again, so the rest of the lab matches.
2-15-color-data
#:package Raylib-cs@8.0.0
using Raylib_cs;
string[] names = { "slime", "lava", "ice", "swamp", "ghost" };
int[] reds = { 120, 230, 150, 90, 200 };
int[] greens = { 220, 90, 210, 110, 190 };
int[] blues = { 80, 40, 245, 60, 230 };
Raylib.InitWindow(800, 450, "Monster Palette");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(24, 26, 34, 255));
for (int i = 0; i < names.Length; i++)
{
DrawSwatch(i, 40 + i * 150);
}
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawSwatch(int index, int x)
{
Color color = new Color(reds[index], greens[index], blues[index], 255);
Raylib.DrawRectangle(x, 40, 110, 70, color);
Raylib.DrawText(names[index], x, 120, 20, Color.White);
}

Task 2: Draw the Numbers Too

  • DrawChannels draws one bar per channel under each swatch, as tall as the channel's value, with the value printed below it. Its bars array holds three named colors, the way the other arrays hold numbers.
  • Make the changes shown and run it. Answer from the chart, not the swatches: which monster has the most blue? Which has the least green? Which has the most even mix of all three? Then check your answers against the swatches.
2-15-color-data
#:package Raylib-cs@8.0.0
using Raylib_cs;
string[] names = { "slime", "lava", "ice", "swamp", "ghost" };
int[] reds = { 120, 230, 150, 90, 200 };
int[] greens = { 220, 90, 210, 110, 190 };
int[] blues = { 80, 40, 245, 60, 230 };
Raylib.InitWindow(800, 450, "Monster Palette");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(24, 26, 34, 255));
for (int i = 0; i < names.Length; i++)
{
DrawSwatch(i, 40 + i * 150);
DrawChannels(i, 40 + i * 150);
}
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawSwatch(int index, int x)
{
Color color = new Color(reds[index], greens[index], blues[index], 255);
Raylib.DrawRectangle(x, 40, 110, 70, color);
Raylib.DrawText(names[index], x, 120, 20, Color.White);
}
void DrawChannels(int index, int x)
{
int[] values = { reds[index], greens[index], blues[index] };
Color[] bars = { Color.Red, Color.Green, Color.Blue };
for (int c = 0; c < 3; c++)
{
int height = values[c] * 4 / 5;
Raylib.DrawRectangle(x + c * 38, 400 - height, 30, height, bars[c]);
Raylib.DrawText($"{values[c]}", x + c * 38, 408, 16, Color.LightGray);
}
}

Task 3: Take the Bits Away

  • Delete DrawChannels and its call, and add DrawRow. It draws the whole palette again at a lower bit depth: levels is how many values each channel may take, and every channel is rounded down to its step.
  • Run it. The top row is the full colors; under it, 4 levels per channel, then 2.
  • Work out how many different colors each row can make, with the arithmetic: 256 levels, 4 levels, 2 levels.
  • Which monster changed most at 4 levels? At 2 levels, which two monsters became the same color, and why? You threw away three quarters of the bits at 4 levels: was the picture three quarters worse?
2-15-color-data
#:package Raylib-cs@8.0.0
using Raylib_cs;
string[] names = { "slime", "lava", "ice", "swamp", "ghost" };
int[] reds = { 120, 230, 150, 90, 200 };
int[] greens = { 220, 90, 210, 110, 190 };
int[] blues = { 80, 40, 245, 60, 230 };
Raylib.InitWindow(800, 450, "Monster Palette");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(24, 26, 34, 255));
for (int i = 0; i < names.Length; i++)
{
DrawSwatch(i, 40 + i * 150);
}
DrawRow(190, 4);
DrawRow(310, 2);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawSwatch(int index, int x)
{
Color color = new Color(reds[index], greens[index], blues[index], 255);
Raylib.DrawRectangle(x, 40, 110, 70, color);
Raylib.DrawText(names[index], x, 120, 20, Color.White);
}
void DrawRow(int y, int levels)
{
int step = 256 / levels;
for (int i = 0; i < names.Length; i++)
{
int r = reds[i] - reds[i] % step;
int g = greens[i] - greens[i] % step;
int b = blues[i] - blues[i] % step;
Raylib.DrawRectangle(40 + i * 150, y, 110, 70, new Color(r, g, b, 255));
}
Raylib.DrawText($"{levels} levels per channel", 40, y + 78, 18, Color.LightGray);
}

Challenge (Optional): Turn the Bit Depth by Hand

  • Keep a variable levels in the main program, starting at 4, and pass it to the second DrawRow call.
  • Make the Up key double it and the Down key halve it, staying between 2 and 256, and show its value on screen. Find the lowest number of levels where you can still tell all five monsters apart.
divider

Checkpoint

Both pictures are screenshots of this page's own programs.

Five color swatches labeled slime, lava, ice, swamp and ghost

Task 1

The palette at full color, at 4 levels per channel, and at 2 levels per channel

Task 3

  • Every color is built with new Color from the four arrays
  • You answered the Task 2 questions from the chart before checking the swatches
  • You worked out the color count for 256, 4 and 2 levels, with the arithmetic
  • You explained which monsters became the same color at 2 levels
divider

Reflection

Answer the following questions before submitting your work.

  1. The swatch and the bars carry the same information. Name one thing each form shows you better than the other.
  2. At 4 levels each channel kept 2 of its 8 bits, and the monsters were still recognizable. What does that suggest about how much of the original data was needed?
  3. Adding a monster meant changing four arrays in step. What could go wrong if you changed three of them and got distracted?
divider

Submit

Submit three things to the dropbox:

  1. Your 2-15-color-data program file, in its Task 3 form.
  2. Your Task 1 and Task 2 answers, and your Task 3 color counts and answers.
  3. Your three reflection answers.

Activity Complete