Sign in to follow this  
BoltBait

Gradient Plugin

Recommended Posts

User bugman was asking about a different type of gradient... so I coded it up!

I also remember someone asking about gradients that had dithering built in. So, I included that.

Also, since I really love HSV gradients, I included that too!

GradientTest.zip

Enjoy.

Obviously it still needs a lot of polish to finish it. But, hopefully some people will find it useful even in its unfinished state.

Here is the CodeLab script:

// Author: BoltBait 
// Submenu: Render 
// Name: Gradients 
// Title: BoltBait's Gradient Test
// URL: http://www.BoltBait.com/pdn 
#region UICode
byte Amount1 = 0; // Method|RGB|HSV
byte Amount2 = 0; // Direction|Vertical|Horizontal
ColorBgra Amount3 = ColorBgra.FromBgr(0,0,0); // From
ColorBgra Amount4 = ColorBgra.FromBgr(255,255,255); // To
bool Amount5 = true; // [0,1] Dither
#endregion

private byte Clamp2Byte(float fValue)
{
   if (fValue<0) return 0;
   if (fValue>255) return 255;
   return (byte)fValue;
}

private byte Clamp2100(float fValue)
{
   if (fValue<0) return 0;
   if (fValue>100) return 100;
   return (byte)fValue;
}

public int Clamp2360(float MyAngle)
{
   // Makes sure that 0.0 <= MyAngle < 360.0
   if (MyAngle >= 360.0)
   {
       MyAngle -= (float)(Math.Floor((double)MyAngle / 360.0) * 360.0);
   }
   if (MyAngle < 0.0)
   {
       MyAngle += (float)360.0;
   }
   return (int)MyAngle;
}

Random R = new Random();

void Render(Surface dst, Surface src, Rectangle rect)
{
   Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
   float v1,v2,v3;
   float f1,f2,f3;
   float t1,t2,t3;
   float d1,d2,d3;
   float maxd, d, a;
   HsvColor hsv;

   // Calculate the maximum size of the fade
   if (Amount2==0)
   {
       maxd = selection.Bottom - selection.Top;
   }
   else
   {
       maxd = selection.Right - selection.Left;
   }

   // Select the starting and ending color of the fade
   if (Amount1==0)  // RGB
   {
       f1 = Amount3.R;
       f2 = Amount3.G;
       f3 = Amount3.B;
       t1 = Amount4.R;
       t2 = Amount4.G;
       t3 = Amount4.B;
   }
   else  // HSV
   {
       hsv = HsvColor.FromColor(Amount3.ToColor());
       f1 = hsv.Hue;
       f2 = hsv.Saturation;
       f3 = hsv.Value;
       hsv = HsvColor.FromColor(Amount4.ToColor());
       t1 = hsv.Hue;
       t2 = hsv.Saturation;
       t3 = hsv.Value;
       // is the shortest path crossing the 0 barrier?
       if (Math.Abs(f1 - t1) > (360-Math.Max(f1,t1)+Math.Min(f1,t1)))
       {
           // wrap ending color
           if (f1 > t1)
           {
               t1 += 360;
           }
           else
           {
               t1 = -360 + t1;
           }
       }
   }

   ColorBgra CurrentPixel;
   for (int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
           CurrentPixel = src[x,y];
           // dithering?
           int adj = 0;
           if (Amount5)
           {
               adj = R.Next(-1,2);
           }
           // calculate current pixel distance
           if (Amount2==0) // Vertical
           {
               d = (180 * (((y+adj)+adj) - selection.Top)) / maxd;
           }
           else    // Horizontal
           {
               d = (180 * (((x+adj)+adj) - selection.Left)) / maxd;
           }
           // calculate percentage of adjustment
           a = (float)(Math.Cos(d * (Math.PI/180)) + 1) / 2;
           // calculate adjustment from first color
           d1 = (t1 - f1) * a;
           d2 = (t2 - f2) * a;
           d3 = (t3 - f3) * a;
           // calculate current color
           v1 = f1 + d1;
           v2 = f2 + d2;
           v3 = f3 + d3;
           // recombine into a color
           if (Amount1 == 0)
           {
               CurrentPixel.R = Clamp2Byte(v1);
               CurrentPixel.G = Clamp2Byte(v2);
               CurrentPixel.B = Clamp2Byte(v3);
           }
           else
           {
               hsv = new HsvColor(Clamp2360(v1),Clamp2100(v2),(int)v3);
               CurrentPixel = ColorBgra.FromColor(hsv.ToColor());
           }
           // make opaque
           CurrentPixel.A = (byte)255;
           // show it
           dst[x,y] = CurrentPixel;
       }
   }
}

I think the from and to colors may be switched. Sorry about that. ;)

  • Upvote 1

Share this post


Link to post
Share on other sites

Ha Ha - beat me to it!

I was revamping one I started ages ago, but didn't finish.

It's got a working angle slider and an ellipse/squirkle option ! - but still work in progress .

Oh well!

( I can PM you my source if you're interested in incorperating the angle in yours) :smile:

Share this post


Link to post
Share on other sites

Lance, I have no idea. I've never used his pack.

I would say that it is most like the gradient plugin I wrote first. (My first plugin.)

Share this post


Link to post
Share on other sites

That might have something to do with the random number function.

Perhaps if it was built as a regular effect instead of a CodeLab effect the random function could be moved out of the render function and somewhere else.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this