c++ hsl to rgb integer

To convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue) in C++, you can follow the steps below:

  1. Include the necessary header files: You will need to include the <cmath> header for mathematical functions and <iostream> for input and output operations.

  2. Define the HSL values: Declare and define the HSL values as variables. The H value represents the hue, S represents the saturation, and L represents the lightness.

  3. Convert HSL to RGB: Use the following formulas to convert HSL to RGB:

  4. Calculate the chroma (C) value by multiplying the saturation (S) with 1 minus the absolute value of 2 times the lightness (L) minus 1.

  5. Calculate the intermediate X value by multiplying the chroma (C) with 1 minus the absolute value of the remainder of the hue (H) divided by 60 degrees minus 1.
  6. Calculate the m value by subtracting the chroma (C) from the lightness (L).
  7. Depending on the value of the hue (H), calculate the red (R), green (G), and blue (B) values as follows:

    • If H is between 0 and 60 degrees, R = C, G = X, and B = 0.
    • If H is between 60 and 120 degrees, R = X, G = C, and B = 0.
    • If H is between 120 and 180 degrees, R = 0, G = C, and B = X.
    • If H is between 180 and 240 degrees, R = 0, G = X, and B = C.
    • If H is between 240 and 300 degrees, R = X, G = 0, and B = C.
    • If H is between 300 and 360 degrees, R = C, G = 0, and B = X.
  8. Scale the RGB values: Multiply the red (R), green (G), and blue (B) values by 255 and round them to the nearest integer to obtain the final RGB values.

  9. Output the RGB values: Print the RGB values to the console or store them in variables for further use.

Here is an example implementation of the above steps:

#include <cmath>
#include <iostream>

int main() {
    // Step 2: Define the HSL values
    double h = 120.0; // Hue
    double s = 0.5;   // Saturation
    double l = 0.5;   // Lightness

    // Step 3: Convert HSL to RGB
    double c = s  (1 - std::abs(2  l - 1));
    double x = c * (1 - std::abs(std::fmod(h / 60.0, 2) - 1));
    double m = l - c / 2.0;

    double r, g, b;
    if (h >= 0 && h < 60) {
        r = c;
        g = x;
        b = 0;
    } else if (h >= 60 && h < 120) {
        r = x;
        g = c;
        b = 0;
    } else if (h >= 120 && h < 180) {
        r = 0;
        g = c;
        b = x;
    } else if (h >= 180 && h < 240) {
        r = 0;
        g = x;
        b = c;
    } else if (h >= 240 && h < 300) {
        r = x;
        g = 0;
        b = c;
    } else {
        r = c;
        g = 0;
        b = x;
    }

    // Step 4: Scale the RGB values
    int red = std::round((r + m) * 255);
    int green = std::round((g + m) * 255);
    int blue = std::round((b + m) * 255);

    // Step 5: Output the RGB values
    std::cout << "RGB values: " << red << ", " << green << ", " << blue << std::endl;

    return 0;
}

This code will convert the HSL values (hue = 120, saturation = 0.5, lightness = 0.5) to RGB values and output them as integers.