Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Gradient Module
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mackie




PostPosted: Fri Jan 25, 2008 8:02 pm   Post subject: Gradient Module

Last night I worked tirelessly to perfect gradient colours. As some may remember from a topic that I have cited in my source. I turned what I have gotten so far into a module. It can do various things, although I plan for quite a few more features for future releases.

Here is an example of the module at work:
Posted Image, might have been reduced in size. Click Image to view fullscreen.

It is fairly simple. So far there are 3 commands.

Gradient.Box(X1, Y1, X2, Y2, LeftColor, RightColor : int, Ratio : real) - This has a linear gradient.
Gradient.BoxUp(X1, Y1, X2, Y2, TopColor, BottomColor : int, Ratio : real) - This has a vertical linear gradient.
Gradient.Oval(X, Y, Radius, OuterColor, InnerColor : int, Ratio : real) - This is has a radial gradient.

The Oval has to be perfectly round until I figure out some more stuff. Also The ratio is still experimental. Ratio is the parameter that controls the prominence of each color. The lower it is the more the right side will be prominent.

It's not perfect, but it's a start. I will be adding more features gradually and releasing them in this thread. I hope you like it, and find a use for it.

Please feel free to give any suggestions or, feature ideas. Module and examples included.



Gradient_Module.zip
 Description:
Version 0.1

Download
 Filename:  Gradient_Module.zip
 Filesize:  46.47 KB
 Downloaded:  278 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
fishtastic




PostPosted: Fri Jan 25, 2008 10:29 pm   Post subject: Re: Gradient Module

Turing:
import Gradient in "gradient.tu"
Draw.FillBox (0, 0, maxx, maxy, black)
loop
    Gradient.Oval (maxx div 2, maxy div 2, 50, Rand.Int (1, 255), Rand.Int (1, 255), 1.0)
    var next := getchar
end loop


When i ran this.
the colour becomes imperfect after few key strokes.
I found that after maxcolour gets greater than around 1000
Turing can no longer return a proper number for the colour. (at least on my computer),

the program itself is awesome, it filled a gap in the standard turing features.

good job! Smile
Mackie




PostPosted: Fri Jan 25, 2008 10:54 pm   Post subject: Re: Gradient Module

Ok, I'm currently working out that glitch. It seems just so odd to me though. I don't see any reason why it is happening. Thanks for pointing it out though. It actually helps a lot, because thats something I wouldn't have tried.
StealthArcher




PostPosted: Sat Jan 26, 2008 12:41 am   Post subject: RE:Gradient Module

I think I'll put my brain to work, and add some stuff.
ericfourfour




PostPosted: Sat Jan 26, 2008 12:55 am   Post subject: RE:Gradient Module

Turing might be limited to a certain amount of colour ids. Once it gets to that limit, it has to pick the closest to what you requested.
StealthArcher




PostPosted: Sat Jan 26, 2008 1:01 am   Post subject: RE:Gradient Module

Yeah, I would know, seeing as in my HBM converter I have to have separate procs for red blue and green grdients.
fishtastic




PostPosted: Sat Jan 26, 2008 2:35 pm   Post subject: RE:Gradient Module

if maxcolour > 1023 then
turing stops returning proper colour number.
this is same when you use Pic.New in turing.

Turing allow you to create new colours or preload pictures but only up to a certain num, this is why turing never included gradient drawing.

only if you can somehow destroy the created colours....
StealthArcher




PostPosted: Sat Jan 26, 2008 2:45 pm   Post subject: Re: Gradient Module

Or make Calculatecolor use RGB.SetColor Instead like so (255,RBeg,GBeg,BBeg) then result 255. Voila, never runs out of colors.

I'll upload some changes and improvements I've made tonight.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Sat Jan 26, 2008 3:31 pm   Post subject: RE:Gradient Module

RGB.SetColour modifies what it bases everything off of. If you use RGB.SetColour, you will have to change how you use whatdotcolour, and all of the other colour related functions.

Really, Turing is the problem. The memory manager for colours was poorly designed. There is a Free procedure for pictures, fonts, etc. The RGB module does not have a Free procedure. When HoltSoft designed the module they should have allowed atleast 2^16 instead of 2^10 colour ids.
fishtastic




PostPosted: Sat Jan 26, 2008 3:56 pm   Post subject: Re: Gradient Module

got it. I was so stupid..

simply replace this
Turing:
function CalculateColor (StartColor, EndColor : int, Ratio : real) : int

        % Variables used to store inputed colors Seperate RGB values.
        var StartRED, StartGREEN, StartBLUE, EndRED, EndGREEN, EndBLUE : real

        % Figures out and stores values from the two inputed values.
        RGB.GetColour (StartColor, StartRED, StartGREEN, StartBLUE)
        RGB.GetColour (EndColor, EndRED, EndGREEN, EndBLUE)

        % Calculates and declares values used for the result.
        var RED : real := StartRED * Ratio + EndRED * (1 - Ratio)
        var GREEN : real := StartGREEN * Ratio + EndGREEN * (1 - Ratio)
        var BLUE : real := StartBLUE * Ratio + EndBLUE * (1 - Ratio)

        % Output Results
        RGB.SetColour (RED, GREEN, BLUE)
    end CalculateColor


with this
Turing:
  function CalculateColor (StartColor, EndColor : int, Ratio : real) : int

        % Variables used to store inputed colors Seperate RGB values.
        var StartRED, StartGREEN, StartBLUE, EndRED, EndGREEN, EndBLUE : real

        % Figures out and stores values from the two inputed values.
        RGB.GetColour (StartColor, StartRED, StartGREEN, StartBLUE)
        RGB.GetColour (EndColor, EndRED, EndGREEN, EndBLUE)

        % Calculates and declares values used for the result.
        var RED : real := StartRED * Ratio + EndRED * (1 - Ratio)
        var GREEN : real := StartGREEN * Ratio + EndGREEN * (1 - Ratio)
        var BLUE : real := StartBLUE * Ratio + EndBLUE * (1 - Ratio)

        % Output Results
        RGB.SetColour (255, RED, GREEN, BLUE)
        result 255
    end CalculateColor


then you can have as many colours as you want. Mr. Green
Mackie




PostPosted: Sat Jan 26, 2008 4:21 pm   Post subject: Re: Gradient Module

Oh wow! how did I miss that before! It seems so simple now. -_- +10 Bits!(For Fishtastic and StealthArcher)
Mackie




PostPosted: Sat Jan 26, 2008 7:15 pm   Post subject: Re: Gradient Module

New Features!

Ok, I just implemented some new features:

Gradient.Star (X, Y, Size, Outer Color, Inner Color : int, Ratio : real) - This can only be drawn as a perfect star for now, The Size is added on to the X and Y to create the second coordinates, it's not like an Oval or a Box.

Gradient.MapleLeaf (X, Y, Size, Outer Color, Inner Color : int, Ratio : real) - This is identical to the Star. Except it's a maple leaf instead.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

alpha.tu - This is a separate module, that I am building on to the gradient module with, it allows for adjustable opacity of an object. In it's current version it can't do much, it has a few levels of opacity that work, and it can be moved like a sprite. It's extremely slow right now. I have it set up in the file 'example_alpha.t' which shows the best results I have gotten so far. Bare with me as this is just out of the starting blocks.

Posted Image, might have been reduced in size. Click Image to view fullscreen.



Gradient_Module(2).zip
 Description:
Version 0.2

Download
 Filename:  Gradient_Module(2).zip
 Filesize:  8.21 KB
 Downloaded:  155 Time(s)

StealthArcher




PostPosted: Sat Jan 26, 2008 11:39 pm   Post subject: Re: Gradient Module

Now I must mess with it, for you opacity is screwed, have no fear, Commodore StealthArbvious is here!
Mackie




PostPosted: Sat Jan 26, 2008 11:59 pm   Post subject: RE:Gradient Module

Haha, I'm trying some serious math on my end. I have gotten an opacity range of 0.0 - 0.3 working perfectly. 0 being solid, and 0.3 being sort of kind of translucent. I just grouped that in with the newest release, as a sort of preview of what's to come.

*cough*
To be done list:
- Alpha Channel. (in beta)
- Selectable gradient styles for ever shape.
- Corner gradient style.(Beta)
- Conical gradient style.
- Reflective Linear gradient style.
- Fix the ratio glitch. (R&D)
- Make shapes radial gradients able to have both a width and height.(R&D)
*cough*
ericfourfour




PostPosted: Sun Jan 27, 2008 12:42 am   Post subject: Re: Gradient Module

The alpha channel is really just a ratio of the foreground to the background. So with an alpha value of 1.0, it will be 100% foreground. With an alpha channel of 0.5, it will be 50% foreground, 50% background. This is not too difficult to do.

I'm going to use my getColour function for simplicities sake:
Turing:
fcn getColour (clr1, clr2 : int, ratio : real) : int
    var r1, g1, b1, r2, g2, b2 : real
    RGB.GetColour (clr1, r1, g1, b1)
    RGB.GetColour (clr2, r2, g2, b2)
    var r : real := r1 * ratio + r2 * (1 - ratio)
    var g : real := g1 * ratio + g2 * (1 - ratio)
    var b : real := b1 * ratio + b2 * (1 - ratio)
    result RGB.AddColour (r, g, b)
end getColour


Now all I have to do is make a function that gets the colour of a pixel (the background colour) and calls the getColour function with that and the specified foreground colour:

Turing:
fcn getAlpaColour (x, y, clr : int, ratio : real) : int
    result getColour (clr, View.WhatDotColour (x, y), ratio)
end getAlpaColour


That wasn't too difficult. Here is an example program using the above code:
Turing:
const STARTX : int := 140
const ENDX : int := 160
const STARTY : int := 140
const ENDY : int := 160
const MIDX : int := (STARTX + ENDX) div 2
const MIDY : int := (STARTY + ENDY) div 2
const BG_CLR : int := yellow
const FG_CLR : int := blue
const TIME_DELAY : int := 10
const ITERATIONS : int := 100

View.Set ("offscreenonly")

for decreasing i : ITERATIONS .. 1
    var ratio : real := (i - 1) / ITERATIONS
    Draw.FillBox (STARTX, STARTY, ENDX, ENDY, white)
    Draw.FillOval (MIDX, MIDY, MIDX - STARTX, MIDY - STARTY, BG_CLR)
    for x : STARTX .. ENDX
        for y : STARTY .. ENDY
            var clr : int := getAlpaColour (x, y, FG_CLR, ratio)
            Draw.Dot (x, y, clr)
        end for
    end for
    View.Update
    Time.Delay (TIME_DELAY)
end for


You should probably change the functions to use RGB.SetColour instead.

This can get more complex: http://en.wikipedia.org/wiki/Alpha_compositing.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 28 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: