st

Kris's build of st
git clone git clone https://git.krisyotam.com/krisyotam/st.git
Log | Files | Refs | README | LICENSE

sixel_hls.c (2860B)


      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) ((255 << 24) + ((r) << 16) + ((g) << 8) +  (b))
     34 
     35 int
     36 hls_to_rgb(int hue, int lum, int sat)
     37 {
     38   double lv = lum / 100.0;
     39   double sv = sat / 100.0;
     40   double c, x, m, c2;
     41   double r1, g1, b1;
     42   int r, g, b, hs;
     43 
     44   hue = (hue + 240) % 360;
     45   if (sat == 0) {
     46     r = g = b = lum * 255 / 100;
     47     return SIXEL_RGB(r, g, b);
     48   }
     49 
     50   if ((c2 = ((2.0 * lv) - 1.0)) < 0.0) {
     51     c2 = -c2;
     52   }
     53   if ((hs = (hue % 120) - 60) < 0) {
     54     hs = -hs;
     55   }
     56   c = (1.0 - c2) * sv;
     57   x = ((60 - hs) / 60.0) * c;
     58   m = lv - 0.5 * c;
     59 
     60   switch (hue / 60) {
     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) * 255.0 + 0.5);
     96   g = (int) ((g1 + m) * 255.0 + 0.5);
     97   b = (int) ((b1 + m) * 255.0 + 0.5);
     98 
     99   if (r < 0) {
    100     r = 0;
    101   } else if (r > 255) {
    102     r = 255;
    103   }
    104   if (g < 0) {
    105     g = 0;
    106   } else if (g > 255) {
    107     g = 255;
    108   }
    109   if (b < 0) {
    110     b = 0;
    111   } else if (b > 255) {
    112     b = 255;
    113   }
    114   return SIXEL_RGB(r, g, b);
    115 }