Hex Generator - FreeCodeCamp Daily Challenge
2 min
Hex Generator: Analysis and Solution
Problem Statement
Given a string representing a CSS color ("red", "green", or "blue"), implement a function that generates a random six-character hexadecimal color code, where the dominant color matches the specified one. If the input is invalid, return exactly "Invalid color".
Requirements:
- Only accepts
"red","green", or"blue"(lowercase). - The dominant component must be strictly greater than the other two.
- The result is a 6-character hexadecimal string (RGB).
- If the input is invalid, return
"Invalid color".
How does the RGB Hexadecimal Format Work?
A hexadecimal color is represented as #RRGGBB, where:
RRare two hexadecimal digits for red (00 to FF)GGfor green (00 to FF)BBfor blue (00 to FF)
For example:
#FF0000 → Pure red (red at max, green and blue at zero)
#00FF00 → Pure green
#0000FF → Pure blueThe function should generate a 6-character string (e.g., FF00AA), where the dominant component is strictly greater than the other two.
Test Cases
| Input | Expected Output | Explanation |
|---|---|---|
| ”yellow" | "Invalid color” | Invalid color |
| ”red” | 6-character string | Valid hex, red dominant |
| ”green” | 6-character string | Valid hex, green dominant |
| ”blue” | 6-character string | Valid hex, blue dominant |
Dominance:
- If the input is “red”, the red value () must be and .
- If it’s “green”, and .
- If it’s “blue”, and .
Flowchart
flowchart TD
A["Input: color ('red', 'green', 'blue')"] --> B{"Valid color?"}
B -- No --> X["Return 'Invalid color'"]
B -- Yes --> C["Generate random values for R, G, B"]
C --> D["Adjust dominant: RR > GG and BB (or similar)"]
D --> E["Convert to hex and concatenate"]
E --> F["Return 6-character string"]
Strategy and Steps
- Validate input: only “red”, “green”, or “blue”.
- Generate random values for R, G, B.
- Adjust the dominant so it is strictly greater.
- Convert each value to two-digit hexadecimal.
- Concatenate and return the string.
Complexity Analysis
Time
- All operations are constant:
Space
- Fixed variable usage:
Edge Cases and Considerations
- Invalid input → returns “Invalid color”
- Dominant never exceeds 255
- The other two values can be equal
- The result is always a 6-character uppercase string
Reflections and Learnings
- Input validation and error handling
- Random number generation and RGB manipulation
- Hexadecimal conversion and padding
- Ensuring color dominance