Check if the given RGB color code is valid or not - GeeksforGeeks

06 Dec, 2021
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=255Output: TrueExplanation: Every color is in range [0, 255]
Input: R=0, G=200, B=355Output: FalseExplanation: 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++
Java
Python3
C#
Javascript
#include <iostream>using namespace std; // Function to check validity// of the color codebool 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 codeint 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;}
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
# Function to check validity# of the color codedef 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 codeif __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
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
<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>
  
true
true
false
false
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
rakeshsahni
ankthon
samim2000
_saurabh_jaiswal
Mathematical
Pattern Searching
Mathematical
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to multiply two matrices
Program to convert a given number to words
Count ways to reach the n'th stair
KMP Algorithm for Pattern Searching
Naive algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Check if a string is substring of another
Boyer Moore Algorithm for Pattern Searching