*/}}
Selaa lähdekoodia

use hsl for wire colors

YimingWu 1 vuosi sitten
vanhempi
commit
ccd0dfe0fa
3 muutettua tiedostoa jossa 36 lisäystä ja 3 poistoa
  1. 3 3
      la_kernel.c
  2. 2 0
      la_tns.h
  3. 31 0
      la_tns_kernel.c

+ 3 - 3
la_kernel.c

@@ -5665,10 +5665,10 @@ void la_RegenerateWireColors(){
     if(MAIN.WireColorCache) free(MAIN.WireColorCache);
     laTheme* t=MAIN.CurrentTheme;
     MAIN.WireColorCache = calloc(1, sizeof(real)*4*MAIN.WireColorSlices);
-    real hcy[]={0.0,0.8,0.6}; hcy[1]=t->WireSaturation; hcy[2]=t->WireBrightness;
+    real hsl[]={0.0,0.8,0.6}; hsl[1]=t->WireSaturation; hsl[2]=t->WireBrightness;
     for(int i=0;i<MAIN.WireColorSlices;i++){
-        hcy[0]=(real)i/(real)MAIN.WireColorSlices;
-        tnsHCY2RGB(hcy, &MAIN.WireColorCache[i*4]); MAIN.WireColorCache[i*4+3]=t->WireTransparency;
+        hsl[0]=(real)i/(real)MAIN.WireColorSlices;
+        tnsHSL2RGB(hsl, &MAIN.WireColorCache[i*4]); MAIN.WireColorCache[i*4+3]=t->WireTransparency;
     }
 }
 void la_SendWireVerts(real x1, real y1, real x2, real y2, real circle_r){

+ 2 - 0
la_tns.h

@@ -1408,6 +1408,8 @@ void tnsHCY2RGBLinear(real *hcy, real *rgb);
 void tnsRGB2HCYLinear(real *rgb, real *hcy);
 void tnsHCY2RGB(real *hcy, real *rgb);
 void tnsRGB2HCY(real *rgb, real *hcy);
+void tnsRGB2HSL(real *rgb, real *hsl);
+void tnsHSL2RGB(real *hsl, real *rgb);
 
 #ifdef __cplusplus
 extern "C"{

+ 31 - 0
la_tns_kernel.c

@@ -4567,6 +4567,37 @@ void tnsRGB2HCY(real *rgb, real *hcy){
     lrgb[2]=_srgb_transfer_function_inv(rgb[2]);
     tnsRGB2HCYLinear(lrgb,hcy);
 }
+static float hue2rgb(float p, float q, float t) {
+  if (t < 0) t += 1; if (t > 1) t -= 1;
+  if (t < 1./6) return p + (q - p) * 6 * t;
+  if (t < 1./2) return q;
+  if (t < 2./3) return p + (q - p) * (2./3 - t) * 6;
+  return p;
+}
+void tnsRGB2HSL(real *rgb, real *hsl) {
+    float max = TNS_MAX3(rgb[0],rgb[1],rgb[2]);
+    float min = TNS_MIN3(rgb[0],rgb[1],rgb[2]);
+    hsl[0]=hsl[1]=hsl[2]=(max+min) / 2;
+    if (max == min) { hsl[0]=hsl[1]=0; }
+    else {
+        float d = max - min;
+        hsl[1] = (hsl[2] > 0.5) ? d / (2 - max - min) : d / (max + min);
+        if (max == rgb[0]) { hsl[0] = (rgb[1] - rgb[2]) / d + (rgb[1] < rgb[2] ? 6 : 0); }
+        else if (max == rgb[1]) { hsl[0] = (rgb[2] - rgb[0]) / d + 2; }
+        else if (max == rgb[2]) { hsl[0] = (rgb[0] - rgb[1]) / d + 4; }
+        hsl[0] /= 6;
+    }
+}
+void tnsHSL2RGB(real *hsl, real *rgb) {
+  if(hsl[1]==0) {rgb[0]=rgb[1]=rgb[2]=hsl[2];}
+  else {
+    float q = hsl[2] < 0.5 ? hsl[2] * (1 + hsl[1]) : hsl[2] + hsl[1] - hsl[2] * hsl[1];
+    float p = 2 * hsl[2] - q;
+    rgb[0] = hue2rgb(p, q, hsl[0] + 1./3);
+    rgb[1] = hue2rgb(p, q, hsl[0]);
+    rgb[2] = hue2rgb(p, q, hsl[0] - 1./3);
+  }
+}
 
 void tnsClearAll(){
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);