fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3.  
  4. uint32_t makeRGB30(float r, float g, float b)
  5. {
  6. const uint32_t mask = (1u << 10u) - 1u;
  7. /* convert float -> uint */
  8. uint32_t rU = r * mask, gU = g * mask, bU = b * mask;
  9. /* combine and return color components */
  10. return ((rU & mask) << 20) | ((gU & mask) << 10) | (bU & mask);
  11. }
  12.  
  13. int main(void)
  14. {
  15. /* samples */
  16. const float colors[][3] = {
  17. { 0.0f, 0.0f, 0.0f }, /* black */
  18. { 1.0f, 0.0f, 0.0f }, /* red */
  19. { 0.0f, 1.0f, 0.0f }, /* green */
  20. { 0.0f, 0.0f, 1.0f }, /* blue */
  21. { 1.0f, 1.0f, 0.0f }, /* yellow */
  22. { 1.0f, 0.0f, 1.0f }, /* magenta */
  23. { 0.0f, 1.0f, 1.0f }, /* cyan */
  24. { 1.0f, 1.0f, 1.0f } /* white */
  25. };
  26. const size_t n = sizeof colors / sizeof *colors;
  27. for (size_t i = 0; i < n; ++i) {
  28. float *color = colors[i];
  29. uint32_t rgb = makeRGB30(color[0], color[1], color[2]);
  30. printf("(%f, %f, %f): %08x\n", color[0], color[1], color[2], rgb);
  31. }
  32. /* done */
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
(0.000000, 0.000000, 0.000000): 00000000
(1.000000, 0.000000, 0.000000): 3ff00000
(0.000000, 1.000000, 0.000000): 000ffc00
(0.000000, 0.000000, 1.000000): 000003ff
(1.000000, 1.000000, 0.000000): 3ffffc00
(1.000000, 0.000000, 1.000000): 3ff003ff
(0.000000, 1.000000, 1.000000): 000fffff
(1.000000, 1.000000, 1.000000): 3fffffff