RGB to Hex Color Converter
In this lab, you'll be writing a Java method named generateHexCode
in the Main
class. The objective is to convert RGB (Red, Green, Blue) color values into their corresponding hexadecimal color code representations.
Task
Your task is to implement the public static String generateHexCode(int red, int green, int blue)
method. This method should take three integer parameters representing the RGB values (ranging from 0 to 255) and return a string representing the hexadecimal color code. The returned string should start with a #
followed by the hexadecimal digits.
Requirements
-
Method Signature: The method should be correctly defined as
public static String generateHexCode(int red, int green, int blue)
in theMain
class. -
Valid Inputs: The RGB values will be integers in the range of 0 to 255.
-
Output Format: The output should be a hexadecimal string starting with
#
.
Edge Cases to Consider
- Minimum Values: When all RGB values are 0, the method should return
#000000
(black). - Maximum Values: When all RGB values are 255, the method should return
#FFFFFF
(white).
Make sure to only return upper case letters as output
Examples
-
Example 1:
- Input:
red = 0, green = 0, blue = 0
- Expected Output:
#000000
- Explanation: The RGB values represent black, and its hexadecimal code is
#000000
.
- Input:
-
Example 2:
- Input:
red = 255, green = 255, blue = 255
- Expected Output:
#FFFFFF
- Explanation: The RGB values represent white, and its hexadecimal code is
#FFFFFF
.
- Input:
Make sure your method handles these cases correctly and test it with other RGB combinations to ensure its accuracy.
Challenges Information
Challenge 1: Generate Hex Code for Black Color
- Objective: Write a method
generateHexCode
in theMain
class. It should return the hexadecimal code for the black color when passed RGB values of 0, 0, 0. - Method Signature: The method should be
public static String generateHexCode(int red, int green, int blue)
. - Expected Behavior: When called with
generateHexCode(0, 0, 0)
, the method should return the string#000000
.
Challenge 2: Generate Hex Code for White Color
- Objective: Modify the
generateHexCode
method in theMain
class to return the hexadecimal code for white color when passed RGB values of 255, 255, 255. - Method Signature: Ensure the method signature remains
public static String generateHexCode(int red, int green, int blue)
. - Expected Behavior: When called with
generateHexCode(255, 255, 255)
, the method should return the string#FFFFFF
.
Challenge 3: Generate Hex Code for Yellow Color
- Objective: Ensure the
generateHexCode
method in theMain
class can return the hexadecimal code for yellow color when passed RGB values of 255, 255, 0. - Method Signature: The method signature should be
public static String generateHexCode(int red, int green, int blue)
. - Expected Behavior: When called with
generateHexCode(255, 255, 0)
, the method should return the string#FFFF00
.