How To change the background color of a container using the Hex format?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 12:38:26

问题


I'm developing a BlackBerry 10 mobile application using the momentics IDE (native SDK).

I want to change the background color of a container using C++. But unfortunately, relating to this [link], you only can define it like below :

**Creating a color in C++:**
Color c1 = Color::fromRGBA(0.5f, 1.0f, 0.2f, 0.8f);
Color c2 = Color::fromARGB(0xff996633);

For the color, I want to use the hex format ("#xxxxxx"). Any one can guide me on this ?


回答1:


Color c2 = Color::fromARGB(0xff996633); is using hex the 0x is c++ representation of a hex code. ff is the A component, 99 is the R, 66 is the G and 33 is the B

So if you want to use the hex value #000099 with no alpha

then it would be

Color::fromARGB(0x00000099)

The following code will convert a string to a hex value, you will need to remove the # from the string before hand however, and then can pass the string into the buffer object

#include <iostream>
#include <sstream>

int main() { 

    std::string hexString("#ffffff");
    hexString.erase(hexString.begin());

    std::istringstream buffer(hexString);

    int value;

    buffer >> std::hex >> value;

    std::cout << std::hex << value;
    return 0;
}


来源:https://stackoverflow.com/questions/22219397/how-to-change-the-background-color-of-a-container-using-the-hex-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!