简体   繁体   中英

How do i check the length for Color.red(c) in android?

As for the RGB value maximum value for each can be 255(length is 3) but for each color. For eg : if Color.red(c) gives the value 12(length is 2) for red then i want to add 0 in front and make it 012 how can i do that. It was possible if i could check the length but i do not know how to do it.

To check the length you can try something like

int red = Color.red(c);
if (red < 10) {
    //0...9 
} else if (red < 100) {
    //10..99
} else {
    //100..255
}

In the each if you can add to the value as many zeros as you want.

String colorLeadingZeroes = String.format("%03d", Color.red(c));

this will force the result to be a string of three digits with leading zeros replacing the spaces

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