簡體   English   中英

用C ++將RGB函數寫入HSL

[英]Writing a function RGB to HSL in c++

我曾嘗試在網站(課程學)上運行此代碼以提交作業,但它總是給我錯誤。

我試圖在Visual Studio代碼上運行此代碼沒有任何問題

#include <math.h>
HSL rgb_to_hsl(int red, int green, int blue)
{
    int hue, sat, lum; // assign the correct values to these variables
    double max, min, red1, green1, blue1;
    red1 = red / 255.0;
    green1 = red / 255.0;
    blue1 = red / 255.0;
    if (red1 == green1 && green1 == blue1)
    {
        hue = 0;
        sat = 0;
        lum = 0;
    }
    else if (red1 > green1 && red1 > blue1)
    {

        max = red1;
        if (green1 > blue1)
        {
            min = blue1;
        }
        else
        {
            min = green1;
        }
    }

    else if (green1 > blue1)
    {

        max = green1;
        if (blue1 > red);
        {

            min = red1;
        }
        else
        {
            min = blue1;
        }
    }

    else
    {

        max = blue1;
        if (green1 > red1)
        {
            min = red1;
        }
        else
        {
            min = green1;
        }
    }
    lum = (max + min) / 2.0;
    if (lum < 0.5)
    {
        sat = (max - min) / (max + min);
    }
    else
    {
        sat = (max - min) / (2 - max - min);
    }
    if (max == red1)
    {
        hue = (green1 - blue1) / (max - min);
    }
    else if (max == green1)
    {
        hue = 2 + (blue1 - red1) / (max - min);
    }
    else
    {
        hue = 4 + (red1 - green1) / (max - min);
        hue = hue * 60;
    }
    if (hue < 0)
    {
        hue += 360;
    }

    lum = (int)round(lum);
    sat = (int)round(sat);
    hue = (int)round(hue);

    // *** Do not edit this line. It is to return 3 values together ***
    return (HSL){.hue = hue, .sat = sat, .lum = lum};
}

這是顯示的錯誤

answer.cc:在函數“ HSL rgb_to_hsl(int,int,int)”中:
answer.cc:171:9:錯誤:在“其他”之前應為“}”
其他
^
answer.cc:在全球范圍內:
answer.cc:177:5:錯誤:“ else”之前應有不合格的ID
其他
^
制作:*** [answer.bin]錯誤1

你放錯地方了; 在這段代碼的if (blue1 > red)之后:

else if (green1 > blue1)
{

    max = green1;
    if (blue1 > red);       // <<--- Here's the problem
    {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM