简体   繁体   中英

Rgb color map to a normalized value

I'm using this function to convert a normalized value between 0 and 1 to an RGB value depending on the JET colormap.

std::vector<double> mapJet(double v, double vmin, double vmax)
{
    if (v < vmin)
        v = vmin;

    if (v > vmax)
        v = vmax;

    double dr, dg, db;

    if (v < 0.1242) {
        db = 0.504 + ((1.-0.504) / 0.1242)*v;
        dg = dr = 0.;
    } else if (v < 0.3747) {
        db = 1.;
        dr = 0.;
        dg = (v - 0.1242) * (1. / (0.3747-0.1242));
    } else if (v < 0.6253) {
        db = (0.6253 - v) * (1. / (0.6253-0.3747));
        dg = 1.;
        dr = (v - 0.3747) * (1. / (0.6253-0.3747));
    } else if (v < 0.8758) {
        db = 0.;
        dr = 1.;
        dg = (0.8758 - v) * (1. / (0.8758-0.6253));
    } else {
        db = 0.;
        dg = 0.;
        dr = 1. - (v - 0.8758) * ((1.-0.504) / (1.-0.8758));
    }

    return std::vector<double> { 255 * dr, 255 * dg, 255 * db };
}

My aim is to find the function double v = mapJet_inv(R,G,B) . That is to say, I convert an RGB color to a normalized value between 0 and 1 depending on the colormap. I tried to start from the end of the mapJet function, however I didn't know how to specify the ranges of the RGB components. Maybe I'm doing this badly. I will appreciate your help. Thank you

vector<float> colors_to_value(vector<float> colors_tab)
{

    double v ,db,dg,dr; v=db=dg=dr=0; vector<float> values_result;

    for(int i=0;i<colors_tab.size();i++)
    {
        dr=colors_tab(i,0)/255.;
        dg=colors_tab(i,1)/255.;
        db=colors_tab(i,2)/255.;

        if ( dg == 0.  &&  dr == 0. ) {
                v = (db - 0.504) / (1.-0.504) * 0.1242; // a revoir

        }
        else if ( db == 1.  &&  dr == 0. ) {
                v = dg/4. + 0.1242;
        }
        else if (db==0 && dr==1)
        {

            v   = 0.8758 -dg*(1. / (0.8758-0.6253)) ;
        }

        else if ( db==0 && dg ==0)
        {
            v    =( 1. - dr ) / ((1.-0.504) / (1.-0.8758)) +  0.8758;
        }
        else{

           float  val1= 0.6253 - db/(1. / (0.6253-0.3747)) ;
           float val2=dr/ (1. / (0.6253-0.3747)) + 0.3747;

             //  v=(val1+val2)/2.;
           if (val1>val2)
                   v=val1;
        }
        if(v<0) v=0;
        values_result.push_back(v);


    }


     return values_result;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM