loading...

re: Daily Challenge #236 - RGB to Hex Conversion VIEW POST

FULL DISCUSSION
 

Here's my solution, with C

int limit_value(int value) {
  if(value <= 0) {
    return 0;
  }
  if(value >= 255) {
    return 255;
  }
  return value;
}

char *rgb(int r, int g, int b) {
  r = limit_value(r);
  g = limit_value(g);
  b = limit_value(b);

  char *result = calloc(7, sizeof(char));
  sprintf(result, "%02X%02X%02X", r, g, b);
  return result;
}
code of conduct - report abuse