Computer Science Canada Gradient Bound by a Shape |
Author: | Mackie [ Thu Jan 24, 2008 6:30 pm ] | ||
Post subject: | Gradient Bound by a Shape | ||
I just started working on a gradient script, and was wondering how I could have the gradient bound by a shape? say a star. I have a square done, and am working on a radial gradient. I was just hoping I could figure something out for odd shapes, even a circle would be cool. Here is what I have so far.
I had some more variables in there to make it look cleaner, but I went more for efficiency. |
Author: | HeavenAgain [ Thu Jan 24, 2008 7:48 pm ] | ||
Post subject: | RE:Gradient Bound by a Shape | ||
![]() |
Author: | Mackie [ Thu Jan 24, 2008 7:50 pm ] |
Post subject: | Re: Gradient Bound by a Shape |
Oh, wow that seems simple enough, how did I not see that. Thanks, I hope to eventually come up with a module. 10 Bits Given. |
Author: | ericfourfour [ Thu Jan 24, 2008 8:02 pm ] |
Post subject: | RE:Gradient Bound by a Shape |
Try making a function that takes two colours and a ratio and returns a new colour. Say red, blue and 0.5 is used to call the function. It will return the colour halfway between red and blue. The algorithm, I believe, looks something like this: new colour = colour1 * (1 - ratio) + colour2 * ratio |
Author: | Mackie [ Thu Jan 24, 2008 8:12 pm ] |
Post subject: | Re: RE:Gradient Bound by a Shape |
ericfourfour @ Thu Jan 24, 2008 8:02 pm wrote: Try making a function that takes two colours and a ratio and returns a new colour.
Say red, blue and 0.5 is used to call the function. It will return the colour halfway between red and blue. The algorithm, I believe, looks something like this: new colour = colour1 * (1 - ratio) + colour2 * ratio Awesome Idea, I was thinking of some way to do that. It will give a lot more functionality to the module. +5 bits |
Author: | Sean [ Thu Jan 24, 2008 9:30 pm ] | ||
Post subject: | Re: Gradient Bound by a Shape | ||
This is what it did...
The box is drawn with the parameters, showing the fading like you wanted, starting at red and decreasing into white by lightening the colour. Also, I believe all you have to do is give it more variables to get other Shapes. Since they all have different values and coordiante layouts. Interest, good work Mackie! |
Author: | StealthArcher [ Thu Jan 24, 2008 10:03 pm ] |
Post subject: | RE:Gradient Bound by a Shape |
Always good to see an innovative student, rather than "Can u do mai wurk fer me lol?". +Bits good stuff, hope it goes soemwhere. |
Author: | ericfourfour [ Thu Jan 24, 2008 10:11 pm ] | ||
Post subject: | Re: Gradient Bound by a Shape | ||
Here is an example of the function I was talking about. I included some sample code so you can see it in action.
The clr parameters are pretty straight forward. The ratio is pretty easy to understand as well. It works by breaking the colours into components and multiplying them by their respecitve ratio. A few examples: When clr1 = blue, clr2 = red and ratio = 0.4, it will return a colour that is 40% blue and 60% red. When clr1 = green, clr2 = yellow and ratio = 0.9, it will return a colour that is 90% green and 10% yellow. |
Author: | Mackie [ Thu Jan 24, 2008 10:20 pm ] |
Post subject: | Re: Gradient Bound by a Shape |
@ericfourfour: That is absolutly spectacular! do you mind if I use your equations for the module? I can most likely implement this immediately. Thanks for the concepts! +5 Bits @StealthArcher: Thanks for the bits! I'm flattered. ![]() |
Author: | StealthArcher [ Thu Jan 24, 2008 10:23 pm ] |
Post subject: | RE:Gradient Bound by a Shape |
If you want some more other ways to see how the rgb miodule was used, check out my hbm converter. |
Author: | ericfourfour [ Thu Jan 24, 2008 11:29 pm ] |
Post subject: | RE:Gradient Bound by a Shape |
It's a pretty common algorithm. If you want to cite something, it would be best to cite this thread. |
Author: | Mackie [ Thu Jan 24, 2008 11:31 pm ] | ||
Post subject: | Re: Gradient Bound by a Shape | ||
Alright, I have one small problem before the ratio's are implemented. It will only show some of the color spectrum. If the start color is black and the end color is white then it will show the spectrum from absolute black, to a more dark gray area. It seems as if I am missing something obvious. At least I got it down to 15 lines. ![]() Here is the module so far, any help would be appreciated. I will continue to work at it as well.
@ericfourfour: Thanks! I will. EDIT: Never mind! I put in the wrong value when running it. ![]() |
Author: | ericfourfour [ Fri Jan 25, 2008 12:06 am ] |
Post subject: | RE:Gradient Bound by a Shape |
You aren't really gaining any efficiency by reducing the number of lines. The more complex something becomes, the more you should focus on readability. One thing that you can easily do to make it more readable, is to make all lines under 80 characters. Also, you should try to use different naming conventions for modules, functions, parameters, variables and constants. I usually go with the Java conventions (CamelCase for classes and modules; camelCase for functions, variables and parameters; and CAMEL_CASE for constants). If you want to go from a lighter black to a darker white, try going from 80% to 20%. If you want it to change faster, decrease the ratio faster. |
Author: | Mackie [ Fri Jan 25, 2008 12:26 am ] |
Post subject: | Re: RE:Gradient Bound by a Shape |
ericfourfour @ Fri Jan 25, 2008 12:06 am wrote: You aren't really gaining any efficiency by reducing the number of lines. The more complex something becomes, the more you should focus on readability. One thing that you can easily do to make it more readable, is to make all lines under 80 characters. Also, you should try to use different naming conventions for modules, functions, parameters, variables and constants. I usually go with the Java conventions (CamelCase for classes and modules; camelCase for functions, variables and parameters; and CAMEL_CASE for constants).
If you want to go from a lighter black to a darker white, try going from 80% to 20%. If you want it to change faster, decrease the ratio faster. Thanks, I'm still unclear on what takes up more resources, when trying to make my code more efficient. I figured the more variables I get rid of the faster it would run. Also, I do plan to use those naming conventions you mentioned. The first release of the module should be out by the week's end. |