check_rgb - 1Fr3aK2/Cub3d GitHub Wiki
📝 check_rgb
This function checks if a given string represents a valid RGB color value in the format "R,G,B", where R, G, and B are integers in the range 0-255.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
rgb |
char* |
A string representing the RGB values separated by commas. |
🔁 Returns
| Return value | Description |
|---|---|
bool |
Returns true if the input string represents a valid RGB value, or false otherwise. |
📖 Description
The check_rgb function validates whether the input string is a correctly formatted RGB value:
- The input string is split into an array using
,as the delimiter. - The function checks if the array has exactly 3 elements (R, G, B). If not, it frees the allocated array and returns
false. - Each element is checked to ensure it is a number using
ft_isnumber. - Each number is validated to be in the acceptable RGB range (0-255) using
check_range. - Memory allocated for the array is freed before returning the result.
The function ensures both the format and the numeric validity of the RGB components.
💡 Example Usage
char *color = "255,128,64";
if (check_rgb(color)) {
printf("Valid RGB color!\n");
} else {
printf("Invalid RGB color.\n");
}