color.cpp
ftxui/screen/color.hpp
array
cmath
cstdint
string
ftxui/screen/color_info.hpp
ftxui/screen/terminal.hpp
ftxui/screen/color.hpp
ftxui/screen/color_info.hpp
ftxui/screen/terminal.hpp
src/ftxui/screen/color.cpp
array
cmath
cstdint
string
ftxui
ftxui::literals

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include"ftxui/screen/color.hpp"

    #include<array>//forarray
    #include<cmath>
    #include<cstdint>
    #include<string>

    #include"ftxui/screen/color_info.hpp"//forGetColorInfo,ColorInfo
    #include"ftxui/screen/terminal.hpp"//forColorSupport,Color,Palette256,TrueColor

    namespaceftxui{
    namespace{
    conststd::array<constchar*,33>palette16code={
    "30","40",//
    "31","41",//
    "32","42",//
    "33","43",//
    "34","44",//
    "35","45",//
    "36","46",//
    "37","47",//
    "90","100",//
    "91","101",//
    "92","102",//
    "93","103",//
    "94","104",//
    "95","105",//
    "96","106",//
    "97","107",//
    };

    }//namespace

    boolColor::operator==(constColor&rhs)const{
    returnred_==rhs.red_&&green_==rhs.green_&&blue_==rhs.blue_&&
    type_==rhs.type_;
    }

    boolColor::operator!=(constColor&rhs)const{
    return!operator==(rhs);
    }

    std::stringColor::Print(boolis_background_color)const{
    if(is_background_color){
    switch(type_){
    caseColorType::Palette1:
    return"49";
    caseColorType::Palette16:
    returnpalette16code[2*red_+1];//NOLINT
    caseColorType::Palette256:
    return"48;5;"+std::to_string(red_);
    caseColorType::TrueColor:
    return"48;2;"+std::to_string(red_)+";"+std::to_string(green_)+
    ";"+std::to_string(blue_);
    }
    }else{
    switch(type_){
    caseColorType::Palette1:
    return"39";
    caseColorType::Palette16:
    returnpalette16code[2*red_];//NOLINT
    caseColorType::Palette256:
    return"38;5;"+std::to_string(red_);
    caseColorType::TrueColor:
    return"38;2;"+std::to_string(red_)+";"+std::to_string(green_)+
    ";"+std::to_string(blue_);
    }
    }
    //NOTREACHED();
    return"";
    }

    ///@briefBuildatransparentcolor.
    ///@ingroupscreen
    Color::Color()=default;

    ///@briefBuildatransparentcolor.
    ///@ingroupscreen
    Color::Color(Palette1/*value*/):Color(){}

    ///@briefBuildacolorusingthePalette16colors.
    ///@ingroupscreen
    Color::Color(Palette16index)
    :type_(ColorType::Palette16),red_(index),alpha_(255){}

    ///@briefBuildacolorusingPalette256colors.
    ///@ingroupscreen
    Color::Color(Palette256index)
    :type_(ColorType::Palette256),red_(index),alpha_(255){
    if(Terminal::ColorSupport()>=Terminal::Color::Palette256){
    return;
    }
    type_=ColorType::Palette16;
    red_=GetColorInfo(Color::Palette256(red_)).index_16;
    }

    ///@briefBuildaColorfromitsRGBrepresentation.
    ///https://en.wikipedia.org/wiki/RGB_color_model
    ///
    ///@paramredThequantityofred[0,255]
    ///@paramgreenThequantityofgreen[0,255]
    ///@paramblueThequantityofblue[0,255]
    ///@paramalphaThequantityofalpha[0,255]
    ///@ingroupscreen
    Color::Color(uint8_tred,uint8_tgreen,uint8_tblue,uint8_talpha)
    :type_(ColorType::TrueColor),
    red_(red),
    green_(green),
    blue_(blue),
    alpha_(alpha){
    if(Terminal::ColorSupport()==Terminal::Color::TrueColor){
    return;
    }

    //FindtheclosestColorfromthedatabase:
    constintmax_distance=256*256*3;
    intclosest=max_distance;
    intbest=0;
    constintdatabase_begin=16;
    constintdatabase_end=256;
    for(inti=database_begin;i<database_end;++i){
    constColorInfocolor_info=GetColorInfo(Color::Palette256(i));
    constintdr=color_info.red-red;
    constintdg=color_info.green-green;
    constintdb=color_info.blue-blue;
    constintdist=dr*dr+dg*dg+db*db;
    if(closest>dist){
    closest=dist;
    best=i;
    }
    }

    if(Terminal::ColorSupport()==Terminal::Color::Palette256){
    type_=ColorType::Palette256;
    red_=best;
    }else{
    type_=ColorType::Palette16;
    red_=GetColorInfo(Color::Palette256(best)).index_16;
    }
    }

    ///@briefBuildaColorfromitsRGBrepresentation.
    ///https://en.wikipedia.org/wiki/RGB_color_model
    ///
    ///@paramredThequantityofred[0,255]
    ///@paramgreenThequantityofgreen[0,255]
    ///@paramblueThequantityofblue[0,255]
    ///@ingroupscreen
    //static
    ColorColor::RGB(uint8_tred,uint8_tgreen,uint8_tblue){
    returnRGBA(red,green,blue,255);
    }

    ///@briefBuildaColorfromitsRGBArepresentation.
    ///https://en.wikipedia.org/wiki/RGB_color_model
    ///@paramredThequantityofred[0,255]
    ///@paramgreenThequantityofgreen[0,255]
    ///@paramblueThequantityofblue[0,255]
    ///@paramalphaThequantityofalpha[0,255]
    ///@ingroupscreen
    ///@seeColor::RGB
    //static
    ColorColor::RGBA(uint8_tred,uint8_tgreen,uint8_tblue,uint8_talpha){
    return{red,green,blue,alpha};
    }

    ///@briefBuildaColorfromitsHSVrepresentation.
    ///https://en.wikipedia.org/wiki/HSL_and_HSV
    ///
    ///@paramhThehueofthecolor[0,255]
    ///@paramsThe"colorfulness"[0,255].
    ///@paramvThe"Lightness"[0,255]
    ///@paramalphaThequantityofalpha[0,255]
    ///@ingroupscreen
    //static
    ColorColor::HSVA(uint8_th,uint8_ts,uint8_tv,uint8_talpha){
    uint8_tregion=h/43;//NOLINT
    uint8_tremainder=(h-(region*43))*6;//NOLINT
    uint8_tp=(v*(255-s))>>8;//NOLINT
    uint8_tq=(v*(255-((s*remainder)>>8)))>>8;//NOLINT
    uint8_tt=(v*(255-((s*(255-remainder))>>8)))>>8;//NOLINT

    //clang-formatoff
    switch(region){//NOLINT
    case0:returnColor(v,t,p,alpha);//NOLINT
    case1:returnColor(q,v,p,alpha);//NOLINT
    case2:returnColor(p,v,t,alpha);//NOLINT
    case3:returnColor(p,q,v,alpha);//NOLINT
    case4:returnColor(t,p,v,alpha);//NOLINT
    case5:returnColor(v,p,q,alpha);//NOLINT
    }//NOLINT
    //clang-formaton
    return{0,0,0,alpha};
    }

    ///@briefBuildaColorfromitsHSVrepresentation.
    ///https://en.wikipedia.org/wiki/HSL_and_HSV
    ///
    ///@paramhThehueofthecolor[0,255]
    ///@paramsThe"colorfulness"[0,255].
    ///@paramvThe"Lightness"[0,255]
    ///@ingroupscreen
    //static
    ColorColor::HSV(uint8_th,uint8_ts,uint8_tv){
    returnHSVA(h,s,v,255);
    }

    //static
    ColorColor::Interpolate(floatt,constColor&a,constColor&b){
    if(a.type_==ColorType::Palette1||//
    b.type_==ColorType::Palette1){
    if(t<0.5F){//NOLINT
    returna;
    }else{
    returnb;
    }
    }

    autoget_color=[](constColor&color,//
    uint8_t*red,uint8_t*green,uint8_t*blue){
    switch(color.type_){
    caseColorType::Palette1:{
    return;
    }

    caseColorType::Palette16:{
    constColorInfoinfo=GetColorInfo(Color::Palette16(color.red_));
    *red=info.red;
    *green=info.green;
    *blue=info.blue;
    return;
    }

    caseColorType::Palette256:{
    constColorInfoinfo=GetColorInfo(Color::Palette256(color.red_));
    *red=info.red;
    *green=info.green;
    *blue=info.blue;
    return;
    }

    caseColorType::TrueColor:
    default:{
    *red=color.red_;
    *green=color.green_;
    *blue=color.blue_;
    return;
    }
    }
    };

    uint8_ta_r=0;
    uint8_ta_g=0;
    uint8_ta_b=0;
    uint8_tb_r=0;
    uint8_tb_g=0;
    uint8_tb_b=0;
    get_color(a,&a_r,&a_g,&a_b);
    get_color(b,&b_r,&b_g,&b_b);

    //Gammacorrection:
    //https://en.wikipedia.org/wiki/Gamma_correction
    autointerp=[t](uint8_ta_u,uint8_tb_u){
    constexprfloatgamma=2.2F;
    constfloata_f=powf(a_u,gamma);
    constfloatb_f=powf(b_u,gamma);
    constfloatc_f=a_f*(1.0F-t)+//
    b_f*t;
    returnstatic_cast<uint8_t>(powf(c_f,1.F/gamma));
    };
    returnColor::RGB(interp(a_r,b_r),//
    interp(a_g,b_g),//
    interp(a_b,b_b));//
    }

    ///@briefBlendtwocolorstogetherusingthealphachannel.
    //static
    ColorColor::Blend(constColor&lhs,constColor&rhs){
    Colorout=Interpolate(float(rhs.alpha_)/255.F,lhs,rhs);
    out.alpha_=lhs.alpha_+rhs.alpha_-lhs.alpha_*rhs.alpha_/255;
    returnout;
    }

    inlinenamespaceliterals{

    Coloroperator""_rgb(unsignedlonglongintcombined){
    //assert(combined<=0xffffffU);
    autoconstred=static_cast<uint8_t>(combined>>16U);
    autoconstgreen=static_cast<uint8_t>(combined>>8U);
    autoconstblue=static_cast<uint8_t>(combined);
    return{red,green,blue};
    }

    }//namespaceliterals

    }//namespaceftxui