Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ hsl to rgb integer

typedef struct RgbColor
{
    unsigned char r;
    unsigned char g;
    unsigned char b;
} RgbColor;

typedef struct HsvColor
{
    unsigned char h;
    unsigned char s;
    unsigned char v;
} HsvColor;

RgbColor HsvToRgb(HsvColor hsv)
{
    RgbColor rgb;
    unsigned char region, remainder, p, q, t;

    if (hsv.s == 0)
    {
        rgb.r = hsv.v;
        rgb.g = hsv.v;
        rgb.b = hsv.v;
        return rgb;
    }

    region = hsv.h / 43;
    remainder = (hsv.h - (region * 43)) * 6; 

    p = (hsv.v * (255 - hsv.s)) >> 8;
    q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
    t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;

    switch (region)
    {
        case 0:
            rgb.r = hsv.v; rgb.g = t; rgb.b = p;
            break;
        case 1:
            rgb.r = q; rgb.g = hsv.v; rgb.b = p;
            break;
        case 2:
            rgb.r = p; rgb.g = hsv.v; rgb.b = t;
            break;
        case 3:
            rgb.r = p; rgb.g = q; rgb.b = hsv.v;
            break;
        case 4:
            rgb.r = t; rgb.g = p; rgb.b = hsv.v;
            break;
        default:
            rgb.r = hsv.v; rgb.g = p; rgb.b = q;
            break;
    }

    return rgb;
}

HsvColor RgbToHsv(RgbColor rgb)
{
    HsvColor hsv;
    unsigned char rgbMin, rgbMax;

    rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
    rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);

    hsv.v = rgbMax;
    if (hsv.v == 0)
    {
        hsv.h = 0;
        hsv.s = 0;
        return hsv;
    }

    hsv.s = 255 * long(rgbMax - rgbMin) / hsv.v;
    if (hsv.s == 0)
    {
        hsv.h = 0;
        return hsv;
    }

    if (rgbMax == rgb.r)
        hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
    else if (rgbMax == rgb.g)
        hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
    else
        hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);

    return hsv;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: what is imposter syndrome 
Cpp :: the partition function of a system is given by z= 1/(1-e^-bEi), calculate the total energy of the system 
Cpp :: clang does not recognize std::cout 
Cpp :: 10011101 
Cpp :: assert warning c++ 
Cpp :: racing horses codechef solution c++ 
Cpp :: 2d vector size c++ 
Cpp :: C is widely used for systems-level software and embedded systems development. 
Cpp :: temporary variable ex c++ 
Cpp :: C++ Point to Every Array Elements 
Cpp :: bitmap rotate 90 deg 
Cpp :: rotateArray 
Cpp :: C++ system("pause") 
Cpp :: 496. Next Greater Element I.cpp 
Cpp :: delete node in a linked list leetcode 
Cpp :: ex:c++ gcc start adress 
Cpp :: c++ max 
Cpp :: print number with leading zeros 
Cpp :: palindrome no example 
Cpp :: longest increasing subsequence nlogn c++ 
Cpp :: c vs c++ vs c# 
Cpp :: cpp set time 
Cpp :: how to shorten code using using c++ in class with typename 
C :: fahrenheit to celsius formula 
C :: classification report to excel 
C :: same project on different monitor in intellij mac 
C :: yourkill071 
C :: random in c 
C :: exclamation mark in c 
C :: string input in c 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =