Setting colors in Hex and Decimal behaving differently

前端 未结 4 1232
攒了一身酷
攒了一身酷 2020-12-31 10:08

I am trying to set an orangish color in the following manner:

WorkSheet.Range(\"A1:A5\").Interior.color = 49407

and

WorkShe         


        
4条回答
  •  星月不相逢
    2020-12-31 10:25

    I believe the answers from Gimp and Johannes both miss the key issue.

    A colour on a computer screen is defined by specifying how much red, blue and green you require. For each of the three colours you specify a number between 0 and 255. These three numbers are normally specified as a single number by concatenating the three separate numbers. With Html you specify a colour as #RRGGBB where RR, GG and BB are hexadecimal numbers or you can replace RRBBGG by the decimal equivalent. With Excel the sequence is reversed so &HBBGGRR or the decimal equivalent.

    49407 is the decimal equivalent of 00C0FF which for Excel means Blue = 0, Green = 192 and Red = 255.

    But &HC0FF or &H00C0FF is -16129 or Blue = 255, Green = 192 and Red = 255. This seems to be a flaw in the &H conversion. I cannot find anyway of getting C0FF converted to 49407.

    If you insist on using the &H conversion, the best I can offer is:

     Range("A1:A5").Interior.color = &H01C0FF
    

    This is Blue = 1, Green = 192 and Red = 255 and I cannot tell the difference from Blue = 0, Green = 192 and Red = 255.

    But I would recommend:

    Range("A1:A5").Interior.color = RGB(255,192,0)
    

    because the RGB function always returns a positive number and you do not have to worry about Excel's reverse sequence.

提交回复
热议问题