]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/gem.c
(no commit message)
[imagemagick] / MagickCore / gem.c
index a4e8df1e206c0c877ab7d4f7b5ed3fb01a59c74f..64608bc357b787c3ac8b966f821624ab5259a734 100644 (file)
@@ -95,7 +95,8 @@ MagickPrivate void ConvertHCLToRGB(const double hue,const double chroma,
     h,
     m,
     r,
-    x;
+    x,
+    z;
 
   /*
     Convert HCL to RGB colorspace.
@@ -111,43 +112,59 @@ MagickPrivate void ConvertHCLToRGB(const double hue,const double chroma,
   b=0.0;
   if ((0.0 <= h) && (h < 1.0))
     {
-      r=c;      
-      g=x;      
+      r=c;
+      g=x;
     }
   else
     if ((1.0 <= h) && (h < 2.0))
       {
-        r=x;      
-        g=c;      
+        r=x;
+        g=c;
       }
     else
       if ((2.0 <= h) && (h < 3.0))
         {
-          g=c;      
-          b=x;      
+          g=c;
+          b=x;
         }
       else
         if ((3.0 <= h) && (h < 4.0))
           {
-            g=x;      
-            b=c;      
+            g=x;
+            b=c;
           }
         else
           if ((4.0 <= h) && (h < 5.0))
             {
-              r=x;      
-              b=c;      
+              r=x;
+              b=c;
             }
           else
             if ((5.0 <= h) && (h < 6.0))
               {
-                r=c;      
-                b=x;      
+                r=c;
+                b=x;
               }
-  m=luma-0.298839*r+0.586811*g+0.114350*b;
-  *red=QuantumRange*(r+m);
-  *green=QuantumRange*(g+m);
-  *blue=QuantumRange*(b+m);
+  m=luma-(0.298839*r+0.586811*g+0.114350*b);
+  /*
+    Choose saturation strategy to clip it into the RGB cube; hue and luma are
+    preserved and chroma may be changed.
+  */
+  z=1.0;
+  if (m < 0.0)
+    {
+      z=luma/(luma-m);
+      m=0.0;
+    }
+  else
+    if ((m+c) > 1.0)
+      {
+        z=(1.0-luma)/(m+c-luma);
+        m=1.0-z*c;
+      }
+  *red=QuantumRange*(z*r+m);
+  *green=QuantumRange*(z*g+m);
+  *blue=QuantumRange*(z*b+m);
 }
 \f
 /*
@@ -474,11 +491,11 @@ MagickPrivate void ConvertRGBToHCL(const double red,const double green,
   max=MagickMax(r,MagickMax(g,b));
   c=max-(double) MagickMin(r,MagickMin(g,b));
   h=0.0;
-  if (c == 0)
+  if (c == 0.0)
     h=0.0;
   else
     if (red == max)
-      h=fmod((g-b)/c,6.0);
+      h=fmod((g-b)/c+6.0,6.0);
     else
       if (green == max)
         h=((b-r)/c)+2.0;