diff --git a/MagickCore/composite.c b/MagickCore/composite.c
index b146756..fa9d3d7 100644
--- a/MagickCore/composite.c
+++ b/MagickCore/composite.c
@@ -220,7 +220,8 @@
     h,
     m,
     r,
-    x;
+    x,
+    z;
 
   /*
     Convert HCL to RGB colorspace.
@@ -269,10 +270,22 @@
                 r=c;
                 b=x;
               }
-  m=luma-(0.298839f*r+0.586811f*g+0.114350f*b);
-  *red=QuantumRange*(r+m);
-  *green=QuantumRange*(g+m);
-  *blue=QuantumRange*(b+m);
+  m=luma-(0.298839*r+0.586811*g+0.114350*b);
+  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);
 }
 
 static void CompositeHCL(const MagickRealType red,const MagickRealType green,
@@ -303,7 +316,7 @@
     h=0.0;
   else
     if (red == max)
-      h=fmod(6.0+(g-b)/c,6.0);
+      h=fmod((g-b)/c+6.0,6.0);
     else
       if (green == max)
         h=((b-r)/c)+2.0;
@@ -312,7 +325,7 @@
           h=((r-g)/c)+4.0;
   *hue=(h/6.0);
   *chroma=QuantumScale*c;
-  *luma=QuantumScale*(0.298839f*r+0.586811f*g+0.114350f*b);
+  *luma=QuantumScale*(0.298839*r+0.586811*g+0.114350*b);
 }
 
 static MagickBooleanType CompositeOverImage(Image *image,