Guest User

Untitled

a guest
Jul 14th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <curses.h>
  5.  
  6. typedef struct {
  7.     int r;
  8.     int g;
  9.     int b;
  10. } RGB;
  11.  
  12. RGB newRGB(int r, int g, int b)
  13. {
  14.     RGB color;
  15.     color.r = r;
  16.     color.g = g;
  17.     color.b = b;
  18.     return color;
  19. }
  20.  
  21. void calcColor(RGB* colors, int totalColors, int currentIndex, float saturation, float lightness);
  22.  
  23. int main()
  24. {
  25.     int totalColors = 60;
  26.     RGB colors[totalColors];
  27.     float saturation = 1.0;
  28.     float lightness = 0.3;
  29.  
  30.     initscr(); // Initialize curses mode
  31.     start_color(); // Initialize colors
  32.  
  33.     int end = totalColors - 18;
  34.     int start = end - 42;
  35.     for (int i = end; i >= start; i--) {
  36.         calcColor(&colors[i], totalColors, i, saturation, lightness);
  37.  
  38.         // Convert RGB colors to curses default model
  39.         int r = colors[i].r * 1000 / 255;
  40.         int g = colors[i].g * 1000 / 255;
  41.         int b = colors[i].b * 1000 / 255;
  42.  
  43.         // Set color in curses
  44.         init_color(i, r, g, b);
  45.         init_pair(i + 16, COLOR_BLACK, i + 16);
  46.  
  47.         // Print the color
  48.         attron(COLOR_PAIR(i));
  49.         printw("  ");
  50.         attroff(COLOR_PAIR(i));
  51.     }
  52.  
  53.     refresh(); // Update the screen
  54.     getch(); // Wait for user input
  55.  
  56.     endwin(); // End curses mode
  57.     return 0;
  58. }
  59.  
  60. void calcColor(RGB* colors, int totalColors, int currentIndex, float saturation, float lightness)
  61. {
  62.     float hue = currentIndex * (360.0 / totalColors);
  63.     float chroma = (1 - fabs(2 * lightness - 1)) * saturation;
  64.     float x = chroma * (1 - fabs(fmod(hue / 60, 2) - 1));
  65.     float m = lightness - chroma / 2;
  66.  
  67.     float r, g, b;
  68.     if (hue >= 0 && hue < 60) {
  69.         r = chroma;
  70.         g = x;
  71.         b = 0;
  72.     } else if (hue >= 60 && hue < 120) {
  73.         r = x;
  74.         g = chroma;
  75.         b = 0;
  76.     } else if (hue >= 120 && hue < 180) {
  77.         r = 0;
  78.         g = chroma;
  79.         b = x;
  80.     } else if (hue >= 180 && hue < 240) {
  81.         r = 0;
  82.         g = x;
  83.         b = chroma;
  84.     } else if (hue >= 240 && hue < 300) {
  85.         r = x;
  86.         g = 0;
  87.         b = chroma;
  88.     } else {
  89.         r = chroma;
  90.         g = 0;
  91.         b = x;
  92.     }
  93.  
  94.     colors->r = (r + m) * 255;
  95.     colors->g = (g + m) * 255;
  96.     colors->b = (b + m) * 255;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment