/**
* Generate a random color in RGB format: rgb(R, G, B)
* This program demonstrates how numbers and bits are used
* to produce valid 8‑bit channel values.
*/
/**
* Create a random 8‑bit integer (0–255).
* Math.random() returns a floating‑point number in [0, 1),
* so multiplying by 256 (2^8) gives a full byte of color data.
*/
function randomChannel(): number {
// 8 bits → values from 0 to 255
return Math.floor(Math.random() * 256);
}
/**
* Produce a random RGB color by combining the channels.
*/
function generateRandomRGB(): {
r: number;
g: number;
b: number;
rgb: string;
} {
const r: number = randomChannel(); // Red channel (8 bits)
const g: number = randomChannel(); // Green channel (8 bits)
const b: number = randomChannel(); // Blue channel (8 bits)
// Construct the CSS-style RGB string
const rgb: string = `rgb(${r}, ${g}, ${b})`;
return { r, g, b, rgb };
}
// Run the program
const result: { r: number; g: number; b: number; rgb: string } = generateRandomRGB();
console.log("Red (8 bits):", result.r);
console.log("Green (8 bits):", result.g);
console.log("Blue (8 bits):", result.b);
console.log("RGB color:", result.rgb);
/*
run:
Red (8 bits): 248
Green (8 bits): 73
Blue (8 bits): 100
RGB color: rgb(248, 73, 100)
*/