My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /***********
  2. * rgb_t.h *
  3. ***********/
  4. /****************************************************************************
  5. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * To view a copy of the GNU General Public License, go to the following *
  18. * location: <http://www.gnu.org/licenses/>. *
  19. ****************************************************************************/
  20. #pragma once
  21. /**
  22. * Implementation of hsl_to_rgb as constexpr functions based on:
  23. *
  24. * https://www.rapidtables.com/convert/color/hsl-to-rgb.html
  25. */
  26. constexpr float _hsl_fmod(float x, float y) {
  27. return x - int(x/y)*y;
  28. }
  29. constexpr float _hsl_c(float, float S, float L) {
  30. return (1.0f - fabs(2*L-1.0f)) * S;
  31. }
  32. constexpr float _hsl_x(float H, float S, float L) {
  33. return _hsl_c(H,S,L) * (1.0f - fabs(_hsl_fmod(H/60, 2) - 1));
  34. }
  35. constexpr float _hsl_m(float H, float S, float L) {
  36. return L - _hsl_c(H,S,L)/2;
  37. }
  38. constexpr float _hsl_rgb(float H, float S, float L, float r, float g, float b) {
  39. return ((uint32_t((r + _hsl_m(H,S,L))*255+0.5) << 16) |
  40. (uint32_t((g + _hsl_m(H,S,L))*255+0.5) << 8) |
  41. (uint32_t((b + _hsl_m(H,S,L))*255+0.5) << 0));
  42. }
  43. constexpr uint32_t hsl_to_rgb(float H, float S, float L) {
  44. return (H < 60) ? _hsl_rgb(H,S,L,_hsl_c(H,S,L), _hsl_x(H,S,L), 0) :
  45. (H < 120) ? _hsl_rgb(H,S,L,_hsl_x(H,S,L), _hsl_c(H,S,L), 0) :
  46. (H < 180) ? _hsl_rgb(H,S,L, 0, _hsl_c(H,S,L), _hsl_x(H,S,L)) :
  47. (H < 240) ? _hsl_rgb(H,S,L, 0, _hsl_x(H,S,L), _hsl_c(H,S,L)) :
  48. (H < 300) ? _hsl_rgb(H,S,L,_hsl_x(H,S,L), 0, _hsl_c(H,S,L)) :
  49. _hsl_rgb(H,S,L,_hsl_c(H,S,L), 0, _hsl_x(H,S,L));
  50. }
  51. /**
  52. * Structure for RGB colors
  53. */
  54. struct rgb_t {
  55. union {
  56. struct {
  57. uint8_t b,g,r,a;
  58. };
  59. uint32_t packed;
  60. };
  61. rgb_t() : packed(0) {}
  62. rgb_t(uint32_t rgb) : packed(rgb) {}
  63. rgb_t(uint8_t r, uint8_t g, uint8_t b) : b(b), g(g), r(r), a(0) {}
  64. operator uint32_t() const {return packed;};
  65. static void lerp(float t, const rgb_t a, const rgb_t b, rgb_t &c) {
  66. c.r = a.r + t * (b.r - a.r);
  67. c.g = a.g + t * (b.g - a.g);
  68. c.b = a.b + t * (b.b - a.b);
  69. }
  70. uint8_t luminance() const {return 0.299*r + 0.587*g + 0.114*b;}
  71. };