Program To Change RGB Color Model To HSV Color ... - GeeksforGeeks

Skip to content geeksforgeeks
  • Courses
    • DSA Courses
    • Programming Languages
  • Tutorials
    • Python Tutorial
      • Python Loops and Control Flow
    • Java
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
    • Algorithms
      • Analysis of Algorithms
      • Searching Algorithms
      • Sorting Algorithms
    • Practice
      • Company Wise Coding Practice
      • Practice Problems Difficulty Wise
      • Language Wise Coding Practice
      • Curated DSA Lists
    • Company Wise SDE Sheets
    • DSA Cheat Sheets
    • Puzzles
  • Data Science
    • Data Science Packages
    • Data Visualization
    • Data Analysis
  • Web Tech
    • Web Development Using Python
      • Django
      • Flask
    • Cheat Sheets
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App Program to Change RGB color model to HSV color model Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Like Article Like Save Share Report Follow

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 additive primary colors, red, green, and blue. HSV Color Model : HSV – (hue, saturation, value), also known as HSB (hue, saturation, brightness), is often used by artists because it is often more natural to think about a color in terms of hue and saturation than in terms of additive or subtractive color components. HSV is a transformation of an RGB colorspace, and its components and colorimetry are relative to the RGB colorspace from which it was derived. Examples :

Input : r, g, b = 45, 215, 0 Output : h, s, v = 107.44186046511628, 100.0, 84.31372549019608 Input : r, g, v = 31, 52, 29 Output : h, s, v = 114.78260869565217, 44.230769230769226, 20.392156862745097

Approach :

  1. Divide r, g, b by 255
  2. Compute cmax, cmin, difference
  3. Hue calculation :
    • if cmax and cmin are equal, then h = 0
    • if cmax equal r then compute h = (60 * ((g – b) / diff) + 360) % 360
    • if cmax equal g then compute h = (60 * ((b – r) / diff) + 120) % 360
    • if cmax equal b then compute h = (60 * ((r – g) / diff) + 240) % 360
  4. Saturation computation :
    • if cmax = 0, then s = 0
    • if cmax does not equal 0 then compute s = (diff/cmax)*100
  5. Value computation :
    • v = cmax*100

Below is the implementation of above approach :

C++

// C++ program change RGB Color // Model to HSV Color Model #include <bits/stdc++.h> using namespace std; void rgb_to_hsv(double r, double g, double b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value double cmax = max(r, max(g, b)); // maximum of r, g, b double cmin = min(r, min(g, b)); // minimum of r, g, b double diff = cmax - cmin; // diff of cmax and cmin. double h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = fmod(60 * ((g - b) / diff) + 360, 360); // if cmax equal g then compute h else if (cmax == g) h = fmod(60 * ((b - r) / diff) + 120, 360); // if cmax equal b then compute h else if (cmax == b) h = fmod(60 * ((r - g) / diff) + 240, 360); // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v double v = cmax * 100; cout << "(" << h << ", " << s << ", " << v << ")" << endl; } // Driver Code int main() { // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); } // This code is contributed by phasing17

Java

// Java program change RGB Color // Model to HSV Color Model class GFG { static void rgb_to_hsv(double r, double g, double b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value double cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b double cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b double diff = cmax - cmin; // diff of cmax and cmin. double h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v double v = cmax * 100; System.out.println("(" + h + " " + s + " " + v + ")"); } // Driver Code public static void main(String[] args) { // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); } } // This code is contributed by PrinciRaj1992

Python3

# Python3 program change RGB Color # Model to HSV Color Model def rgb_to_hsv(r, g, b): # R, G, B values are divided by 255 # to change the range from 0..255 to 0..1: r, g, b = r / 255.0, g / 255.0, b / 255.0 # h, s, v = hue, saturation, value cmax = max(r, g, b) # maximum of r, g, b cmin = min(r, g, b) # minimum of r, g, b diff = cmax-cmin # diff of cmax and cmin. # if cmax and cmax are equal then h = 0 if cmax == cmin: h = 0 # if cmax equal r then compute h elif cmax == r: h = (60 * ((g - b) / diff) + 360) % 360 # if cmax equal g then compute h elif cmax == g: h = (60 * ((b - r) / diff) + 120) % 360 # if cmax equal b then compute h elif cmax == b: h = (60 * ((r - g) / diff) + 240) % 360 # if cmax equal zero if cmax == 0: s = 0 else: s = (diff / cmax) * 100 # compute v v = cmax * 100 return h, s, v ''' Driver Code ''' # print(rgb_to_hsv(45, 215, 0)) # print(rgb_to_hsv(31, 52, 29)) print(rgb_to_hsv(129, 88, 47))

C#

// C# program change RGB Color // Model to HSV Color Model using System; class GFG { static void rgb_to_hsv(double r, double g, double b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value double cmax = Math.Max(r, Math.Max(g, b)); // maximum of r, g, b double cmin = Math.Min(r, Math.Min(g, b)); // minimum of r, g, b double diff = cmax - cmin; // diff of cmax and cmin. double h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v double v = cmax * 100; Console.WriteLine("(" + h + " " + s + " " + v + ")"); } // Driver Code public static void Main(String[] args) { // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); } } // This code is contributed by Rajput-Ji

Javascript

<script> // javascript program change RGB Color // Model to HSV Color Model function rgb_to_hsv(r , g , b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value var cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b var cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b var diff = cmax - cmin; // diff of cmax and cmin. var h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v var v = cmax * 100; document.write("(" + h.toFixed(1) + ", " + s + ", " + v + ")"); } // Driver Code // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); // This code is contributed by todaysgaurav </script>

Output

(30, 63.5659, 50.5882)

Time Complexity: O(1)Auxiliary Space: O(1), As constant extra space is used.

S

shrikanth13 Follow Improve Previous Article Changing the contrast and brightness of an image using Python - OpenCV Next Article

Similar Reads

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 available for a color. Approach: First, check if 9 min read 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: RG 10 min read Count of arrangements of RGB balls with no duplicates in a set Given R red balls, G green balls, B blue balls. A set can have 1, 2, or 3 balls. Also, a set could have all balls of the same color or all balls of different colors. All other possible sets are not considered valid. The task is to calculate the minimum possible sets required to place all balls. Examples: Input: R = 4, G = 2, B = 4Output: 4Explanati 7 min read Minimize cost to travel from source to destination in a Matrix based on given row change and column change cost Given an M*N grid, and given an array startPos[], indicating that the starting position is the cell (startPos[0], startPos[1]) and the array homePos[] indicating its destination is at the cell's (homePos[0], homePos[1]). From any cell movement is allowed only in four directions: left, right, up, and down, and cannot go outside the boundary. There a 7 min read Problem solving on Boolean Model and Vector Space Model Boolean Model: It is a simple retrieval model based on set theory and boolean algebra. Queries are designed as boolean expressions which have precise semantics. Retrieval strategy is based on binary decision criterion. Boolean model considers that index terms are present or absent in a document. Problem Solving: Consider 5 documents with a vocabula 4 min read Color a grid such that all same color cells are connected either horizontally or vertically Given three integers R, C, N, and an array arr[] of size N. The task is to color all cells of a grid of R rows and C columns such that all same color cells are connected either horizontally or vertically. N represents the colors numbered from 1 to N and arr[] denotes the quantity of each color. The total quantity of color is exactly equal to the to 8 min read Min cost to color all walls such that no adjacent walls have same color There is a row of N walls in Geeksland. The king of Geeksland ordered Alexa to color all the walls on the occasion of New Year. Alexa can color each wall with one of the K colors. The cost associated with coloring each wall with a particular color is represented by a 2D costs array of size N * K. For example, costs[0][0] is the cost of coloring wal 15 min read Color N boxes using M colors such that K boxes have different color from the box on its left Given N number of boxes arranged in a row and M number of colors. The task is to find the number of ways to paint those N boxes using M colors such that there are exactly K boxes with a color different from the color of the box on its left. Print this answer modulo 998244353.Examples: Input: N = 3, M = 3, K = 0 Output: 3 Since the value of K is zer 15+ 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 colors. You might, for example, guess YRGB. When 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 resistor. Program to find the resistance of 4-Band 14 min read How to implement decrease key or change key in Binary Search Tree? Given a Binary Search Tree, write a function that takes the following three as arguments: Root of tree Old key value New Key Value The function should change old key value to new key value. The function may assume that old key-value always exists in Binary Search Tree. Example: Input: Root of below tree 50 / \ 30 70 / \ / \ 20 40 60 80 Old key valu 15+ min read Change string to a new character set Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd....xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. Examples: New character set : qwertyuiopasdfghjklzxcvbnm Input : "utta" Output : geek Input : "egrt" Output : 7 min read Zip function in Python to change to a new character set Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd….xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. Examples: New character set : qwertyuiopasdfghjklzxcvbnm Input : "utta" Output : geek Input : "egrt" Output : co 2 min read Check if X can give change to every person in the Queue Given an array of N integers where Ai denotes the currency of note that the i-th person has. The possible currencies are 5, 10, and 20. All the N people are standing in a queue waiting to buy an ice cream from X which costs Rs 5. Initially, X has an initial balance of 0. Check if X will be able to provide change for all people who are waiting to bu 7 min read Number of ways to change the XOR of two numbers by swapping the bits Given two binary strings s1 and s2. The XOR of them is X, the task is to find the number of ways to swap two-bit positions in string s1 such that XOR formed between new s1 and s2 is not same as X. Examples: Input: s1 = "01011", s2 = "11001" Output: 4 swap bits of index(1-based) (1, 4), (2, 3), (3, 4), or (3, 5) such that XOR value is changed. Input 8 min read Change K elements so that (a1^2 + a2^2 + …+ aN^2 ) <= (a1 + a2 +…+ aN) becomes true Given an array Arr of size N. The task is to tell whether it is possible to change at most K elements of this sequence to arbitrary positive integers in such a way that the below condition holds. Examples: Input:N = 2, Arr[] = {1, 2}, K = 2 Output: Possible (As A[2] can be change to 1) Input: N = 2, Arr[] = {5, 6}, K = 1 Output: Not Possible (As we 4 min read Overall percentage change from successive changes Given an array Arr which represent the percentage change. The task is to determine the Percentage increase after these percentage change. Examples: Input: arr[] = {10, 20, 30, 10} Output: Percentage change is = 88.76 % Input: arr[] = {20, 15, 9, 7} Output: Percentage change is = 60.94 % Solution without successive change:- let us take a number N = 6 min read Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve Given an array arr of size n, the task is to find the minimum number of elements that should be changed (element value can be changed to anything) so that there exists an index 0 ? i ? n-2 such that: All the numbers in range 0 to i (inclusive) are < 0.All numbers in range i+1 to n-1 (inclusive) are > 0. Examples: Input: arr[] = {-1, -2, -3, 3 6 min read Minimum moves required to change position with the given operation Given two integers S and T and an array arr that contains elements from 1 to N in unsorted fashion. The task is to find the minimum number of moves to move Sth element to the Tth place in the array with the following operation: A single move consists of the following // Initially b[] = {1, 2, 3, ..., N} // arr[] is input array for (i = 1..n) temp[a 5 min read Buy minimum items without change and given coins You have an unlimited number of 10-rupee coins and exactly one coin of r rupee and you need to buy minimum items each of cost k such that you do not ask for change.Examples: Input: k = 15, r = 2 Output: 2 You should buy two cables and pay 2*15=30 rupees. It is obvious that you can pay this sum without any change.Input: k = 237, r = 7 Output:1 It is 4 min read Minimum operations required to change the array such that |arr[i] - M| <= 1 Given an array arr[] of integers, the task is to find the minimum number of operations required to change the array elements such that for any positive integer M, |arr[i] - M| ? 1 for all valid i. In a single operation, any element of the array can either be incremented or decremented by 1.Examples: Input: arr[] = {10, 1, 4} Output: 7 If we change 6 min read Change one element in the given array to make it an Arithmetic Progression Given an array which is an original arithmetic progression with one element changed. The task is to make it an arithmetic progression again. If there are many such possible sequences, return any one of them. The length of the array will always be greater than 2. Examples: Input : arr = [1, 3, 4, 7] Output : arr = [1, 3, 5, 7] The common difference 7 min read Coin Change | BFS Approach Given an integer X and an array arr[] of length N consisting of positive integers, the task is to pick minimum number of integers from the array such that they sum up to N. Any number can be chosen infinite number of times. If no answer exists then print -1.Examples: Input: X = 7, arr[] = {3, 5, 4} Output: 2 The minimum number elements will be 2 as 6 min read Check if the bracket sequence can be balanced with at most one change in the position of a bracket Given an unbalanced bracket sequence as a string str, the task is to find whether the given string can be balanced by moving at most one bracket from its original place in the sequence to any other position.Examples: Input: str = ")(()" Output: Yes As by moving s[0] to the end will make it valid. "(())"Input: str = "()))(()" Output: No Approach: Co 6 min read Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 Given a bracket sequence as a string str, the task is to find whether the given string can be balanced by moving at most one bracket from its original place in the sequence to any other position.Examples: Input: str = ")(()" Output: Yes As by moving s[0] to the end will make it valid. "(())"Input: str = "()))(()" Output: No Approach: The problem ca 5 min read Percentage change in Hemisphere volume if radius is changed Given that the radius of a hemisphere is changed by a fixed percentage so, the target is to calculate the percentage changed in the volume of the hemisphere. Examples: Input: r = 20% Output: 72.80%Input: r = 70% Output: 391.30 % Approach: Let, the radius of the hemisphere = [Tex]a [/Tex] Given percentage increase = [Tex]x% [/Tex] Volume before incr 5 min read Count ways to change direction of edges such that graph becomes acyclic Given a directed and unweighted graph consisting of N vertices and an array arr[] where ith vertex has a directed edge to arr[i]. The task is to find the number of ways to change the direction of edges such that the given graph is acyclic. Examples: Input: N = 3, arr[] = {2, 3, 1} The directed graph form by the given information is: Output: 6 Expla 12 min read Find the percentage change in the area of a Rectangle Given two integers P and Q which represents the percentage change in the length and breadth of the rectangle, the task is to print the percentage change in the area of the rectangle. Examples: Input: P = 10, Q = 20 Output: 32 Explanation: Let the initial length of the rectangle be 100 and breadth be 80. Initial area = 8000. New length = 110 and new 4 min read Change in Median of given array after deleting given elements Given two arrays arr1[] and arr2[]. The array arr1[] is sorted. The task is to print the change in the median after removing each element from array arr2[] one by one. Note: The array arr2[] has only those elements that are present in array arr1[]. Examples: Input: arr1[] = {2, 4, 6, 8, 10}, arr2[] = {4, 6} Output: 1 1 Explanation: Initially median 7 min read Minimum steps to change N to 1 by changing it to 2*N or N/10 at any step Given an integer N, find the minimum number of operations to change N to 1. If not possible, print -1. One operation is defined as either converting N to the number 2*N or converting N to the number N/10(only if N is divisible by 10). Examples: Input: N = 50Output: 3Explanation: N can be converted to 1 in 3 steps as follows:-> Firstly, multiply 5 min read Article Tags :
  • DSA
  • Mathematical
  • Technical Scripter
  • Basic Coding Problems
Practice Tags :
  • Mathematical
Like three90RightbarBannerImg Explore More We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Lightbox Improvement Suggest changes Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. geeksforgeeks-suggest-icon Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. geeksforgeeks-improvement-icon Suggest Changes min 4 words, max CharLimit:2000

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences

Từ khóa » Hsv Color Space Calculator