#include #include #include unsigned short getRed(unsigned long); unsigned short getGreen(unsigned long); unsigned short getBlue(unsigned long); void makeHCC(unsigned long r, unsigned long g, unsigned long b, char hex[]); void makeHex(unsigned long n, char hex[]); int main() { unsigned long num, combo; unsigned short red, green, blue; unsigned short cred=0, cgreen=0, cblue=0; char chex[8]; const unsigned long MAX = pow(2,32)-1; char ch; // printf("sizeof(num) is %ld and MAX is %ld\n", sizeof(num), MAX); puts("enter three integers between 0 and 2^32 - 1"); ch = getchar(); // repeat three times for (int count = 1; count <= 3; count++) { // read num if (! isdigit(ch)) // bad input return 1; num = 0; while (isdigit(ch)) { num = num * 10 + ch - '0'; ch = getchar(); } if (count < 3) while (isspace(ch)) { ch = getchar(); } if (num > MAX) // bad input return 2; red = getRed(num); green = getGreen(num); blue = getBlue(num); printf("num is %ld == rgb(%d, %d, %d) (for testing: == #%08lx)\n", num, red, green, blue, num); if (count == 1) cred = red; if (count == 2) cgreen = green; if (count == 3) cblue = blue; } // for each number combo = (((cred << 8) + cgreen) << 8) + cblue; makeHex(combo, chex); // makeHCC(cred, cgreen, cblue, chex); printf("combo color is rgb(%d, %d, %d) == %s == %ld\n", cred, cgreen, cblue, chex, combo); return 0; } unsigned short getRed(unsigned long n) { // n = n & 0x00FF0000; // mask the red bits // return (unsigned short) (n >> 16); n = n >> 16; n = n & 0x000000FF; return (unsigned short) n; } unsigned short getGreen(unsigned long n) { n = n >> 8; n = n & 0x000000FF; // mask the green bits return (unsigned short) n; } unsigned short getBlue(unsigned long n) { n = n & 0x000000FF; // mask the blue bits return (unsigned short) n; } void makeHex(unsigned long n, char hex[]) { sprintf(hex,"#%06lx", n); } void makeHCC(unsigned long r, unsigned long g, unsigned long b, char hex[]) { char color[3]; hex[0] = '#'; sprintf(color, "%02lx", r); hex[1] = color[0]; hex[2] = color[1]; sprintf(color, "%02lx", g); hex[3] = color[0]; hex[4] = color[1]; sprintf(color, "%02lx", b); hex[5] = color[0]; hex[6] = color[1]; hex[7] = '\0'; }