Jump to content


Photo

Gradient Plugin


  • Please log in to reply
11 replies to this topic

#1 BoltBait

BoltBait
  • Administrators
  • 8,910 posts
  • LocationCalifornia, USA
  • Reputation:103

Posted 27 April 2012 - 12:57 AM

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!

Attached File  GradientTest.zip   5.06K   150 downloads

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. ;)
Click to play:
Posted ImagePosted ImagePosted ImagePosted ImagePosted Image
Download: BoltBait's Plugin Pack | CodeLab | More... and how about a Computer Dominos Game

#2 Red ochre

Red ochre
  • Members
  • 533 posts
  • LocationSurrey, England
  • Reputation:62

Posted 27 April 2012 - 02:00 AM

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:
Posted Image
Red ochre Plugin pack............................................................... Diabolical Drawings

#3 Ego Eram Reputo

Ego Eram Reputo

    Master of Competition Ideas and 2012 Proton Award Winner

  • Moderators
  • 5,635 posts
  • LocationNorth Canterbury, New Zealand
  • Reputation:220

Posted 27 April 2012 - 02:49 AM

@Boltbait: Why are you still inisisting on rolling your own Clamp2Byte function? Rick hasn't moved the current one for ages :lol:

#4 BoltBait

BoltBait
  • Administrators
  • 8,910 posts
  • LocationCalifornia, USA
  • Reputation:103

Posted 27 April 2012 - 02:52 AM

Hey, I don't have to learn a lesson more than once. ;)
Click to play:
Posted ImagePosted ImagePosted ImagePosted ImagePosted Image
Download: BoltBait's Plugin Pack | CodeLab | More... and how about a Computer Dominos Game

#5 Lance McKnight

Lance McKnight
  • Members
  • 1,230 posts
  • Reputation:4

Posted 08 May 2012 - 12:38 AM

Bolt, how is this different from the gradient plug-in from KrisVDM's pack?

Officially retired from this forum. Have a nice day.


#6 BoltBait

BoltBait
  • Administrators
  • 8,910 posts
  • LocationCalifornia, USA
  • Reputation:103

Posted 08 May 2012 - 04:31 PM

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.)
Click to play:
Posted ImagePosted ImagePosted ImagePosted ImagePosted Image
Download: BoltBait's Plugin Pack | CodeLab | More... and how about a Computer Dominos Game

#7 Tanel

Tanel
  • Members
  • 215 posts
  • Reputation:6

Posted 14 May 2012 - 04:24 PM

Dithering disappears at some point when sliding the nub around in (either) color wheel.

#8 BoltBait

BoltBait
  • Administrators
  • 8,910 posts
  • LocationCalifornia, USA
  • Reputation:103

Posted 14 May 2012 - 05:22 PM

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.
Click to play:
Posted ImagePosted ImagePosted ImagePosted ImagePosted Image
Download: BoltBait's Plugin Pack | CodeLab | More... and how about a Computer Dominos Game

#9 AhmedElyamani

AhmedElyamani
  • Members
  • 205 posts
  • LocationEgypt
  • Reputation:8

Posted 31 August 2012 - 04:09 PM

it's nice but it's only dithering the top 26 pixels :P

#10 RFX

RFX
  • Members
  • 129 posts
  • Reputation:11

Posted 08 October 2012 - 02:11 AM

I also want to create a fade plug-in with the same effect bugman mentioned but with a few other options so I posted my idea here Plug-In Advice - How do I create a "fade plug-in". I was referred to the gradient plug in and am looking forward to checking it out! Thanks BoltBait! :D

rf2ooosiggy5.png


#11 Ego Eram Reputo

Ego Eram Reputo

    Master of Competition Ideas and 2012 Proton Award Winner

  • Moderators
  • 5,635 posts
  • LocationNorth Canterbury, New Zealand
  • Reputation:220

Posted 08 October 2012 - 05:13 AM

Not quite. You were referred to the Gradient Tool :GradientTool: This tool does the functions you were requesting - no plugin required.

#12 RFX

RFX
  • Members
  • 129 posts
  • Reputation:11

Posted 15 October 2012 - 08:10 PM

lol thanks Ego Eram Reputo! I should lay off the blunts a bit till I get used to where everything is. xD

rf2ooosiggy5.png