📜  检查给定的 RGB 颜色代码是否有效

📅  最后修改于: 2022-05-13 01:56:09.624000             🧑  作者: Mango

检查给定的 RGB 颜色代码是否有效

给定三个数字RGB作为红色,绿色和蓝色的颜色代码,分别以RGB 颜色代码的形式。任务是知道给定的颜色代码是否有效。

注意:当所有数字都在 [0, 255] 范围内时,颜色代码有效。

例子:

方法:要检查颜色代码是否有效,我们需要检查每个颜色是否在 [0, 255] 范围内。如果任何颜色不在此范围内,则返回 false,否则返回 true。

下面是该方法的实现:

C++
#include 
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



输出
true
true
false
false

时间复杂度: O(1)
辅助空间: O(1)