Check if the given RGB color code is valid or not
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;
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 ;
}
int main()
{
int R, G, B;
R = 0, G = 0, B = 0;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n" ;
R = 0, G = 100, B = 255;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n" ;
R = 0, G = 200, B = 355;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n" ;
R = -100, G = 0, B = 255;
(isValidRGB(R, G, B)) ? cout << "true\n"
: cout << "false\n" ;
return 0;
}
|
Java
class GFG {
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 ;
}
public static void main(String args[]) {
int R, G, B;
R = 0 ;
G = 0 ;
B = 0 ;
if (isValidRGB(R, G, B))
System.out.println( "true" );
else
System.out.println( "false" );
R = 0 ;
G = 100 ;
B = 255 ;
if (isValidRGB(R, G, B))
System.out.println( "true" );
else
System.out.println( "false" );
R = 0 ;
G = 200 ;
B = 355 ;
if (isValidRGB(R, G, B))
System.out.println( "true" );
else
System.out.println( "false" );
R = - 100 ;
G = 0 ;
B = 255 ;
if (isValidRGB(R, G, B))
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
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 ;
if __name__ = = "__main__" :
R = 0 ; G = 0 ; B = 0 ;
if isValidRGB(R, G, B) :
print ( "true" )
else :
print ( "false" )
R = 0 ; G = 100 ; B = 255 ;
if isValidRGB(R, G, B) :
print ( "true" )
else :
print ( "false" )
R = 0 ; G = 200 ; B = 355 ;
if isValidRGB(R, G, B) :
print ( "true" )
else :
print ( "false" )
R = - 100 ; G = 0 ; B = 255 ;
if isValidRGB(R, G, B) :
print ( "true" )
else :
print ( "false" )
|
C#
using System;
public class GFG {
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 ;
}
public static void Main( string []args) {
int R, G, B;
R = 0;
G = 0;
B = 0;
if (isValidRGB(R, G, B))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
R = 0;
G = 100;
B = 255;
if (isValidRGB(R, G, B))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
R = 0;
G = 200;
B = 355;
if (isValidRGB(R, G, B))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
R = -100;
G = 0;
B = 255;
if (isValidRGB(R, G, B))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
|
Javascript
<script>
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 ;
}
let R, G, B;
R = 0, G = 0, B = 0;
(isValidRGB(R, G, B)) ? document.write( "true<br/>" )
: document.write( "false<br/>" );
R = 0, G = 100, B = 255;
(isValidRGB(R, G, B)) ? document.write( "true<br/>" )
: document.write( "false<br/>" );
R = 0, G = 200, B = 355;
(isValidRGB(R, G, B)) ? document.write( "true<br/>" )
: document.write( "false<br/>" );
R = -100, G = 0, B = 255;
(isValidRGB(R, G, B)) ? document.write( "true<br/>" )
: document.write( "false<br/>" );
</script>
|
Output
true
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;
bool isValidRGB(string colorCode) {
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]?)\\)$" );
return regex_match(colorCode, pattern);
}
int main() {
string colorCode = "rgb(0, 0, 0)" ;
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n" ;
colorCode = "rgb(0, 100, 255)" ;
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n" ;
colorCode = "rgb(0, 200, 355)" ;
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n" ;
colorCode = "rgb(-100, 0, 255)" ;
(isValidRGB(colorCode)) ? cout << "true\n" : cout << "false\n" ;
return 0;
}
|
Java
import java.util.regex.*;
public class GFG {
public static boolean isValidRGB(String colorCode)
{
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]?)\\)$" ;
Pattern regexPattern = Pattern.compile(pattern);
Matcher matcher = regexPattern.matcher(colorCode);
return matcher.matches();
}
public static void main(String[] args)
{
String colorCode = "rgb(0, 0, 0)" ;
System.out.println(isValidRGB(colorCode));
colorCode = "rgb(0, 100, 255)" ;
System.out.println(isValidRGB(colorCode));
colorCode = "rgb(0, 200, 355)" ;
System.out.println(isValidRGB(colorCode));
colorCode = "rgb(-100, 0, 255)" ;
System.out.println(isValidRGB(colorCode));
}
}
|
Python3
import re
def is_valid_rgb(color_code):
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]?)\)$'
return re.match(pattern, color_code) is not None
if __name__ = = "__main__" :
color_code = "rgb(0, 0, 0)"
print ( "true" if is_valid_rgb(color_code) else "false" )
color_code = "rgb(0, 100, 255)"
print ( "true" if is_valid_rgb(color_code) else "false" )
color_code = "rgb(0, 200, 355)"
print ( "true" if is_valid_rgb(color_code) else "false" )
color_code = "rgb(-100, 0, 255)"
print ( "true" if is_valid_rgb(color_code) else "false" )
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool IsValidRGB( string colorCode)
{
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]?)\)$" );
return pattern.IsMatch(colorCode);
}
public static void Main()
{
string colorCode = "rgb(0, 0, 0)" ;
Console.WriteLine(IsValidRGB(colorCode));
colorCode = "rgb(0, 100, 255)" ;
Console.WriteLine(IsValidRGB(colorCode));
colorCode = "rgb(0, 200, 355)" ;
Console.WriteLine(IsValidRGB(colorCode));
colorCode = "rgb(-100, 0, 255)" ;
Console.WriteLine(IsValidRGB(colorCode));
}
}
|
Javascript
function isValidRGB(colorCode) {
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]?)\)$/;
return pattern.test(colorCode);
}
let colorCode = "rgb(0, 0, 0)" ;
console.log(isValidRGB(colorCode));
colorCode = "rgb(0, 100, 255)" ;
console.log(isValidRGB(colorCode));
colorCode = "rgb(0, 200, 355)" ;
console.log(isValidRGB(colorCode));
colorCode = "rgb(-100, 0, 255)" ;
console.log(isValidRGB(colorCode));
|
Output
true
true
false
false
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
15 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...