1 /* guia07/ej08.c 2 * por Martin J. Klöckner 3 * github.com/klewer-martin 4 */ 5 6 #include <stdio.h> 7 8 #define MASK_RED 0xFF0000 9 #define MASK_GREEN 0x00FF00 10 #define MASK_BLUE 0x0000FF 11 12 #define SHIFT_RED 16 13 #define SHIFT_GREEN 8 14 #define SHIFT_BLUE 0 15 16 typedef unsigned int uint; 17 typedef unsigned char uchar; 18 typedef enum { OK, ERROR_BRIGHTNESS_OUT_OF_RANGE, ERROR_NULL_POINTER } status_t; 19 20 uchar get_red(uint color); 21 uchar get_green(uint color); 22 uchar get_blue(uint color); 23 24 status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue); 25 status_t rgb_components2(uint color, uchar *red, uchar *green, uchar *blue); 26 27 uint mix_colors(uint color1, uint color2); 28 uint change_brightness(uint *color, float brightness); 29 uint complementary_color(uint color); 30 31 int main (void) 32 { 33 uint color1 = 0xFF6464; 34 uint color2 = 0x0100FF; 35 36 printf("color1: %06X\n", color1); 37 printf("color2: %06X\n", color2); 38 printf("color2: %06X\n", complementary_color(color1)); 39 40 printf("%X\n", mix_colors(color1, color2)); 41 42 change_brightness(&color1, 1); 43 44 printf("%d red\n", get_red(color1)); 45 printf("%d green\n", get_green(color1)); 46 printf("%d blue\n", get_blue(color1)); 47 48 return OK; 49 } 50 51 uchar get_red(uint color) 52 { 53 return (uchar)((color & MASK_RED) >> SHIFT_RED); 54 } 55 56 uchar get_green(uint color) 57 { 58 return (uchar)((color & MASK_GREEN) >> SHIFT_GREEN); 59 } 60 61 uchar get_blue(uint color) 62 { 63 return (uchar)((color & MASK_BLUE) >> SHIFT_BLUE); 64 } 65 66 /* This implementation its independendent from other functions */ 67 status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue) 68 { 69 if(!red || !green || !blue) return ERROR_NULL_POINTER; 70 71 *red = (uchar)((color & MASK_RED) >> SHIFT_RED); 72 *green = (uchar)((color & MASK_GREEN) >> SHIFT_GREEN); 73 *blue = (uchar)((color & MASK_BLUE) >> SHIFT_BLUE); 74 75 return OK; 76 } 77 78 /* This implementation insted depends from other functions */ 79 status_t rgb_components2(uint color, uchar *red, uchar *green, uchar *blue) 80 { 81 if(!red || !green || !blue) return ERROR_NULL_POINTER; 82 83 *red = get_red(color); 84 *green = get_green(color); 85 *blue = get_blue(color); 86 87 return OK; 88 } 89 90 uint mix_colors(uint color1, uint color2) 91 { 92 uint aux, res; 93 94 /* Suma color con color, y lo va asignando en res, en caso de ser mayor al maximo 255, asigna 255 al color que corresponda */ 95 res = ((aux = ((color1 & MASK_RED) + (color2 & MASK_RED))) > MASK_RED) ? MASK_RED : aux; 96 res += ((aux = ((color1 & MASK_GREEN) + (color2 & MASK_GREEN))) > MASK_GREEN) ? MASK_GREEN : aux; 97 res += ((aux = ((color1 & MASK_BLUE) + (color2 & MASK_BLUE))) > MASK_BLUE) ? MASK_BLUE : aux; 98 99 return res; 100 } 101 102 status_t change_brightness(uint *color, float brightness) 103 { 104 if(!color) return ERROR_NULL_POINTER; 105 else if(brightness < 0 || brightness > 100) return ERROR_BRIGHTNESS_OUT_OF_RANGE; 106 107 /* Convierte el valor de brillo en porcentual */ 108 brightness *= .01; 109 110 /* Multiplica cada color con brightness */ 111 *color = ((int)(((*color & MASK_RED) >> SHIFT_RED) * brightness) << SHIFT_RED) + (*color & ~MASK_RED); 112 *color = ((int)(((*color & MASK_GREEN) >> SHIFT_GREEN) * brightness) << SHIFT_GREEN) + (*color & ~MASK_GREEN); 113 *color = ((int)(((*color & MASK_BLUE) >> SHIFT_BLUE) * brightness) << SHIFT_BLUE) + (*color & ~MASK_BLUE); 114 115 return OK; 116 } 117 118 uint complementary_color(uint color) 119 { 120 return (MASK_RED - (color & MASK_RED)) + (MASK_GREEN - (color & MASK_GREEN)) + (MASK_BLUE - (color & MASK_BLUE)); 121 }