st-flexipatch

My st-flexipatch configuration
git clone git://git.ethandl.dev/st-flexipatch
Log | Files | Refs | README | LICENSE

sixel_hls.c (2897B)


      1 // sixel.c (part of mintty)
      2 // this function is derived from a part of graphics.c
      3 // in Xterm pl#310 originally written by Ross Combs.
      4 //
      5 // Copyright 2013,2014 by Ross Combs
      6 //
      7 //                         All Rights Reserved
      8 //
      9 // Permission is hereby granted, free of charge, to any person obtaining a
     10 // copy of this software and associated documentation files (the
     11 // "Software"), to deal in the Software without restriction, including
     12 // without limitation the rights to use, copy, modify, merge, publish,
     13 // distribute, sublicense, and/or sell copies of the Software, and to
     14 // permit persons to whom the Software is furnished to do so, subject to
     15 // the following conditions:
     16 //
     17 // The above copyright notice and this permission notice shall be included
     18 // in all copies or substantial portions of the Software.
     19 //
     20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     23 // IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
     24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     27 //
     28 // Except as contained in this notice, the name(s) of the above copyright
     29 // holders shall not be used in advertising or otherwise to promote the
     30 // sale, use or other dealings in this Software without prior written
     31 // authorization.
     32 
     33 #define SIXEL_RGB(r, g, b) ((r) + ((g) << 8) + ((b) << 16) + (255 << 24))
     34 
     35 int
     36 hls_to_rgb(int hue, int lum, int sat)
     37 {
     38   double hs = (hue + 240) % 360;
     39   double hv = hs / 360.0;
     40   double lv = lum / 100.0;
     41   double sv = sat / 100.0;
     42   double c, x, m, c2;
     43   double r1, g1, b1;
     44   int r, g, b;
     45   int hpi;
     46 
     47   if (sat == 0) {
     48     r = g = b = lum * 255 / 100;
     49     return SIXEL_RGB(r, g, b);
     50   }
     51 
     52   if ((c2 = ((2.0 * lv) - 1.0)) < 0.0) {
     53     c2 = -c2;
     54   }
     55   c = (1.0 - c2) * sv;
     56   hpi = (int) (hv * 6.0);
     57   x = (hpi & 1) ? c : 0.0;
     58   m = lv - 0.5 * c;
     59 
     60   switch (hpi) {
     61   case 0:
     62     r1 = c;
     63     g1 = x;
     64     b1 = 0.0;
     65     break;
     66   case 1:
     67     r1 = x;
     68     g1 = c;
     69     b1 = 0.0;
     70     break;
     71   case 2:
     72     r1 = 0.0;
     73     g1 = c;
     74     b1 = x;
     75     break;
     76   case 3:
     77     r1 = 0.0;
     78     g1 = x;
     79     b1 = c;
     80     break;
     81   case 4:
     82     r1 = x;
     83     g1 = 0.0;
     84     b1 = c;
     85     break;
     86   case 5:
     87     r1 = c;
     88     g1 = 0.0;
     89     b1 = x;
     90     break;
     91   default:
     92     return SIXEL_RGB(255, 255, 255);
     93   }
     94 
     95   r = (int) ((r1 + m) * 100.0 + 0.5);
     96   g = (int) ((g1 + m) * 100.0 + 0.5);
     97   b = (int) ((b1 + m) * 100.0 + 0.5);
     98 
     99   if (r < 0) {
    100     r = 0;
    101   } else if (r > 100) {
    102     r = 100;
    103   }
    104   if (g < 0) {
    105     g = 0;
    106   } else if (g > 100) {
    107     g = 100;
    108   }
    109   if (b < 0) {
    110     b = 0;
    111   } else if (b > 100) {
    112     b = 100;
    113   }
    114   return SIXEL_RGB(r * 255 / 100, g * 255 / 100, b * 255 / 100);
    115 }