Warning, /include/Geant4/tools/hls is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_hls
0005 #define tools_hls
0006
0007 namespace tools {
0008
0009 //T is float or double.
0010
0011 template <class T>
0012 inline T hls_to_rgb2(T rn1,T rn2,T huei) {
0013 // taken from CERN-ROOT/TColor.
0014 T hue = huei;
0015 if (hue > 360) hue = hue - 360;
0016 if (hue < 0) hue = hue + 360;
0017 if (hue < 60 ) return rn1 + (rn2-rn1)*hue/60;
0018 if (hue < 180) return rn2;
0019 if (hue < 240) return rn1 + (rn2-rn1)*(240-hue)/60;
0020 return rn1;
0021 }
0022
0023 template <class T>
0024 inline void hls_to_rgb(T hue,T light,T satur,T& a_r,T& a_g,T& a_b){
0025 // taken from CERN-ROOT/TColor.
0026 // Static method to compute RGB from HLS. The l and s are between [0,1]
0027 // and h is between [0,360]. The returned r,g,b triplet is between [0,1].
0028
0029 T rh, rl, rs, rm1, rm2;
0030 rh = rl = rs = 0;
0031
0032 if (hue > 0) rh = hue;
0033 if (rh > 360) rh = 360;
0034
0035 if (light > 0) rl = light;
0036 if (rl > 1) rl = 1;
0037
0038 if (satur > 0) rs = satur;
0039 if (rs > 1) rs = 1;
0040
0041 if (rl <= T(0.5)) {
0042 rm2 = rl*(1 + rs);
0043 } else {
0044 rm2 = rl + rs - rl*rs;
0045 }
0046 rm1 = 2*rl - rm2;
0047
0048 if (!rs) { a_r = rl; a_g = rl; a_b = rl; return; }
0049 a_r = hls_to_rgb2(rm1, rm2, rh+120);
0050 a_g = hls_to_rgb2(rm1, rm2, rh);
0051 a_b = hls_to_rgb2(rm1, rm2, rh-120);
0052 }
0053
0054 template <class T>
0055 inline void rgb_to_hls(T rr,T gg,T bb,T& hue,T& light,T& satur) {
0056 // The r,g,b triplet is between
0057 // [0,1], hue is between [0,360], light and satur are [0,1].
0058
0059 T rnorm, gnorm, bnorm, minval, maxval, msum, mdiff, cr, cg, cb;
0060 minval = maxval = 0;
0061 cr = cg = cb = 0;
0062
0063 if (rr > 0) cr = rr;
0064 if (cr > 1) cr = 1;
0065
0066 if (gg > 0) cg = gg;
0067 if (cg > 1) cg = 1;
0068
0069 if (bb > 0) cb = bb;
0070 if (cb > 1) cb = 1;
0071
0072 minval = cr;
0073 if (cg < minval) minval = cg;
0074 if (cb < minval) minval = cb;
0075 maxval = cr;
0076 if (cg > maxval) maxval = cg;
0077 if (cb > maxval) maxval = cb;
0078
0079 rnorm = gnorm = bnorm = 0;
0080 mdiff = maxval - minval;
0081 msum = maxval + minval;
0082 light = T(0.5) * msum;
0083 if (maxval != minval) {
0084 rnorm = (maxval - cr)/mdiff;
0085 gnorm = (maxval - cg)/mdiff;
0086 bnorm = (maxval - cb)/mdiff;
0087 } else {
0088 satur = hue = 0;
0089 return;
0090 }
0091
0092 if (light < T(0.5))
0093 satur = mdiff/msum;
0094 else
0095 satur = mdiff/(2 - msum);
0096
0097 if (cr == maxval)
0098 hue = 60 * (6 + bnorm - gnorm);
0099 else if (cg == maxval)
0100 hue = 60 * (2 + rnorm - bnorm);
0101 else
0102 hue = 60 * (4 + gnorm - rnorm);
0103
0104 if (hue > 360)
0105 hue = hue - 360;
0106 }
0107
0108
0109 }
0110
0111 #endif