Check if the given RGB color code is valid or not
Last Updated :
15 Nov, 2023
Given three numbers R, G and B as the color code for Red, Green and Blue respectively as in the form of RGB color code. The task is to know whether the given color code is valid or not.
RGB Format: The RGB(Red, Green, Blue) format is used to define the color of an HTML element by specifying the R, G, B values range between 0 to 255. For example: RGB value of Red color is (255, 0, 0), Green color is (0, 255, 0), Blue color is (0, 0, 255) etc.
color: rgb(R, G, B);
Note: The color code is valid when all numbers are in the range [0, 255].
Examples:
Input: R=0, G=100, B=255
Output: True
Explanation: Every color is in range [0, 255]
Input: R=0, G=200, B=355
Output: False
Explanation: Blue is not in range [0, 255]
Approach: To check if the color code is valid or not we need to check if each of the color is in the range [0, 255] or not. If any of the color is not in this range, return false, else return true.
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
// Function to check validity
// of the color code
bool isValidRGB(int R, int G, int B)
{
if (R < 0 || R > 255)
return false;
else if (G < 0 || G > 255)
return false;
else if (B < 0 || B > 255)
return false;
else
return true;
}
// Driver code
int main()
{
int R, G, B;
// Check if rgb(0, 0, 0) is valid or not
R = 0, G = 0, B = 0;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n";
// Check if rgb(0, 100, 255) is valid or not
R = 0, G = 100, B = 255;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n";
// Check if rgb(0, 200, 355) is valid or not
R = 0, G = 200, B = 355;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n";
// Check if rgb(-100, 0, 255) is valid or not
R = -100, G = 0, B = 255;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n";
return 0;
}
Java
class GFG {
// Function to check validity
// of the color code
public static boolean isValidRGB(int R, int G, int B) {
if (R < 0 || R > 255)
return false;
else if (G < 0 || G > 255)
return false;
else if (B < 0 || B > 255)
return false;
else
return true;
}
// Driver code
public static void main(String args[]) {
int R, G, B;
// Check if rgb(0, 0, 0) is valid or not
R = 0;
G = 0;
B = 0;
if (isValidRGB(R, G, B))
System.out.println("true");
else
System.out.println("false");
// Check if rgb(0, 100, 255) is valid or not
R = 0;
G = 100;
B = 255;
if (isValidRGB(R, G, B))
System.out.println("true");
else
System.out.println("false");
// Check if rgb(0, 200, 355) is valid or not
R = 0;
G = 200;
B = 355;
if (isValidRGB(R, G, B))
System.out.println("true");
else
System.out.println("false");
// Check if rgb(-100, 0, 255) is valid or not
R = -100;
G = 0;
B = 255;
if (isValidRGB(R, G, B))
System.out.println("true");
else
System.out.println("false");
}
}
// This code is contributed by saurabh_jaiswal
Python3
# Function to check validity
# of the color code
def isValidRGB(R, G, B) :
if (R < 0 or R > 255) :
return False;
elif (G < 0 or G > 255) :
return False;
elif (B < 0 or B > 255) :
return False;
else :
return True;
# Driver code
if __name__ == "__main__" :
# Check if rgb(0, 0, 0) is valid or not
R = 0; G = 0; B = 0;
if isValidRGB(R, G, B) :
print("true")
else :
print("false")
# Check if rgb(0, 100, 255) is valid or not
R = 0; G = 100; B = 255;
if isValidRGB(R, G, B) :
print("true")
else :
print("false")
# Check if rgb(0, 200, 355) is valid or not
R = 0; G = 200; B = 355;
if isValidRGB(R, G, B) :
print("true")
else :
print("false")
# Check if rgb(-100, 0, 255) is valid or not
R = -100; G = 0; B = 255;
if isValidRGB(R, G, B) :
print("true")
else :
print("false")
# This code is contributed by AnkThon
C#
using System;
public class GFG {
// Function to check validity
// of the color code
public static bool isValidRGB(int R, int G, int B) {
if (R < 0 || R > 255)
return false;
else if (G < 0 || G > 255)
return false;
else if (B < 0 || B > 255)
return false;
else
return true;
}
// Driver code
public static void Main(string []args) {
int R, G, B;
// Check if rgb(0, 0, 0) is valid or not
R = 0;
G = 0;
B = 0;
if (isValidRGB(R, G, B))
Console.WriteLine("true");
else
Console.WriteLine("false");
// Check if rgb(0, 100, 255) is valid or not
R = 0;
G = 100;
B = 255;
if (isValidRGB(R, G, B))
Console.WriteLine("true");
else
Console.WriteLine("false");
// Check if rgb(0, 200, 355) is valid or not
R = 0;
G = 200;
B = 355;
if (isValidRGB(R, G, B))
Console.WriteLine("true");
else
Console.WriteLine("false");
// Check if rgb(-100, 0, 255) is valid or not
R = -100;
G = 0;
B = 255;
if (isValidRGB(R, G, B))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Function to check validity
// of the color code
const isValidRGB = (R, G, B) => {
if (R < 0 || R > 255)
return false;
else if (G < 0 || G > 255)
return false;
else if (B < 0 || B > 255)
return false;
else
return true;
}
// Driver code
let R, G, B;
// Check if rgb(0, 0, 0) is valid or not
R = 0, G = 0, B = 0;
(isValidRGB(R, G, B)) ? document.write("true<br/>")
: document.write("false<br/>");
// Check if rgb(0, 100, 255) is valid or not
R = 0, G = 100, B = 255;
(isValidRGB(R, G, B)) ? document.write("true<br/>")
: document.write("false<br/>");
// Check if rgb(0, 200, 355) is valid or not
R = 0, G = 200, B = 355;
(isValidRGB(R, G, B)) ? document.write("true<br/>")
: document.write("false<br/>");
// Check if rgb(-100, 0, 255) is valid or not
R = -100, G = 0, B = 255;
(isValidRGB(R, G, B)) ? document.write("true<br/>")
: document.write("false<br/>");
// This code is contributed by rakeshsahni
</script>
Outputtrue
true
false
false
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 2: Using Regular Expression:
- This approach uses regular expressions to match valid RGB color codes.
- The regular expression pattern ^rgb\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\)$ matches a string that starts with "rgb(", followed by three comma-separated integers (each representing a color component), and ends with ")".
- The integers are matched using regex character classes to ensure that they are between 0 and 255 (inclusive).
- The \s* in between the commas and integers matches any whitespace characters (including none) between them. The ^ and $ at the beginning and end of the pattern ensure that the entire string is matched, i.e., there are no extraneous characters before or after the color code.
Here is the code of above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
// Function to check validity of the color code
bool isValidRGB(string colorCode) {
// Regular expression to match valid RGB color codes
regex pattern("^rgb\\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\)$");
// Check if the color code matches the pattern
return regex_match(colorCode, pattern);
}
// Driver code
int main() {
// Check if rgb(0, 0, 0) is valid or not
string colorCode = "rgb(0, 0, 0)";
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n";
// Check if rgb(0, 100, 255) is valid or not
colorCode = "rgb(0, 100, 255)";
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n";
// Check if rgb(0, 200, 355) is valid or not
colorCode = "rgb(0, 200, 355)";
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n";
// Check if rgb(-100, 0, 255) is valid or not
colorCode = "rgb(-100, 0, 255)";
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n";
return 0;
}
Java
import java.util.regex.*;
public class GFG {
// Function to check validity of the color code
public static boolean isValidRGB(String colorCode)
{
// Regular expression to match valid RGB color codes
String pattern
= "^rgb\\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\)$";
// Create a Pattern object
Pattern regexPattern = Pattern.compile(pattern);
// Create a Matcher object
Matcher matcher = regexPattern.matcher(colorCode);
// Check if the color code matches the pattern
return matcher.matches();
}
// Driver code
public static void main(String[] args)
{
// Check if rgb(0, 0, 0) is valid or not
String colorCode = "rgb(0, 0, 0)";
System.out.println(isValidRGB(colorCode));
// Check if rgb(0, 100, 255) is valid or not
colorCode = "rgb(0, 100, 255)";
System.out.println(isValidRGB(colorCode));
// Check if rgb(0, 200, 355) is valid or not
colorCode = "rgb(0, 200, 355)";
System.out.println(isValidRGB(colorCode));
// Check if rgb(-100, 0, 255) is valid or not
colorCode = "rgb(-100, 0, 255)";
System.out.println(isValidRGB(colorCode));
}
}
// This code is contributed by Taranpreet Singh.
Python3
import re
# Function to check validity of the color code
def is_valid_rgb(color_code):
# Regular expression to match valid RGB color codes
pattern = r'^rgb\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\)$'
# Check if the color code matches the pattern
return re.match(pattern, color_code) is not None
# Driver code
if __name__ == "__main__":
# Check if rgb(0, 0, 0) is valid or not
color_code = "rgb(0, 0, 0)"
print("true" if is_valid_rgb(color_code) else "false")
# Check if rgb(0, 100, 255) is valid or not
color_code = "rgb(0, 100, 255)"
print("true" if is_valid_rgb(color_code) else "false")
# Check if rgb(0, 200, 355) is valid or not
color_code = "rgb(0, 200, 355)"
print("true" if is_valid_rgb(color_code) else "false")
# Check if rgb(-100, 0, 255) is valid or not
color_code = "rgb(-100, 0, 255)"
print("true" if is_valid_rgb(color_code) else "false")
# This code is contributed by Taranpreet Singh.
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
// Function to check the validity of the color code
public static bool IsValidRGB(string colorCode)
{
// Regular expression to match valid RGB color codes
Regex pattern = new Regex(
@"^rgb\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\)$");
// Check if the color code matches the pattern
return pattern.IsMatch(colorCode);
}
public static void Main()
{
// Check if rgb(0, 0, 0) is valid or not
string colorCode = "rgb(0, 0, 0)";
Console.WriteLine(IsValidRGB(colorCode));
// Check if rgb(0, 100, 255) is valid or not
colorCode = "rgb(0, 100, 255)";
Console.WriteLine(IsValidRGB(colorCode));
// Check if rgb(0, 200, 355) is valid or not
colorCode = "rgb(0, 200, 355)";
Console.WriteLine(IsValidRGB(colorCode));
// Check if rgb(-100, 0, 255) is valid or not
colorCode = "rgb(-100, 0, 255)";
Console.WriteLine(IsValidRGB(colorCode));
}
}
JavaScript
// Function to check validity of the color code
function isValidRGB(colorCode) {
// Regular expression to match valid RGB color codes
const pattern = /^rgb\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?),\s*(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\)$/;
// Check if the color code matches the pattern
return pattern.test(colorCode);
}
// Example usage
let colorCode = "rgb(0, 0, 0)";
console.log(isValidRGB(colorCode)); // Output: true
colorCode = "rgb(0, 100, 255)";
console.log(isValidRGB(colorCode)); // Output: true
colorCode = "rgb(0, 200, 355)";
console.log(isValidRGB(colorCode)); // Output: false
colorCode = "rgb(-100, 0, 255)";
console.log(isValidRGB(colorCode)); // Output: false
Outputtrue
true
false
false
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if a given string is a valid Hexadecimal Color Code or not
Given a string str, the task is to check whether the given string is an HTML Hex Color Code or not. Print Yes if it is, otherwise print No. Examples: Input: str = â#1AFFa1âOutput: Yes Input: str = â#F00âOutput: Yes Input: str = �âOutput: No Approach: An HTML Hex Color Code follows the below-mentione
7 min read
Convert the given RGB color code to Hex color code
Given three colors, such as R, G, and B, convert these RGB color to a hex color code. If the conversion is not possible, print -1. Examples: Input: R = 0, G = 0, B = 0 Output: #000000 Input: R = 255, G = 255, B = 256 Output: -1 Explanation: A 256 color code is not possible as only the 0-255 range is
9 min read
Program to calculate Resistance using given color code in circuits
There are many different types of Resistor available which can be used in both electrical and electronic circuits. The resistance value, tolerance, and wattage rating are generally printed onto the body of the resistor as color bands. The task is to find the resistance of 4-band resistor and 5-band
14 min read
Program to Change RGB color model to HSV color model
Given RGB color range, our task is to convert RGB color to HSV color.RGB Color Model : The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three
9 min read
How to validate Hexadecimal Color Code using Regular Expression
Given string str, the task is to check whether the string is valid hexadecimal colour code or not by using Regular Expression. The valid hexadecimal color code must satisfy the following conditions. It should start from '#' symbol.It should be followed by the letters from a-f, A-F and/or digits from
6 min read
Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
Given 3 integers R, G, and B denoting the count of 3 colors Red, Green, and Blue respectively such that two different colors of the same quantity(say X) combine to form a third color of twice that quantity 2 * X. The task is to check if it is possible to convert all the colors to a single color or n
6 min read
RGYB(color) Slots Game to guess the correct color for the correct slot
Given that you have four slots, and each slot will contain a color red (R), yellow (Y), green (G), blue (B) respectively. For example, if you select YGGR (Slot-1 is yellow, Slots-2 and -3 are green, Slot -4 is red). The colors of slots are not known to you beforehand. You will make a guess about the
9 min read
Count of ordered triplets (R, G, B) in a given original string
Given a string S of size N, the task is to count all possible triplets in the given string which consists of only 3 colours (R)Red, (G)Green and (B)Blue in the order (R, G, B). Examples: Input: S = "RRGB" Output: 2 Explanation: There are two triplets of RGB in the given string: R at index 0, G at in
8 min read
HTML | DOM Input Color value Property
The DOM Input Color value Property in HTML DOM is used to set or return the value of the value attribute of a color picker. The value attribute is used to specify the color for the color picker. It's default value is #000000 (black color). Syntax It returns the value property.colorObject.valueIt is
2 min read
M-Coloring Problem in Python
M-Coloring Problem is a classic algorithmic problem which involves coloring the vertices of a graph using at most M different colors such that no two adjacent vertices share the same color. We are given a graph and an integer M, determine if it's possible to color the graph using M colors such that
4 min read