Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Help reducing the amount of lines calling rgb

Author
Message
Lukas W
21
Years of Service
User Offline
Joined: 5th Sep 2003
Location: Sweden
Posted: 4th Jan 2016 04:17
Hey Forum!
After several months, I opened dbpro again and started to write something, anything really..
It struck me when after 4 hours into the adventure I had barely done anything at all and upon this discovery, I decided to do some cleaning and remove useless crap.

Well, I can't find a way to make this ugly wall of text disappear. Err, without slapping it inside a function and hiding it somewhere.


I tried to play around with the dword values from rgb() (essentially trying to skip the xxx_r, xxx_g, xxx_b variables altogether) but I was too stupid - as is displayed here:


If it is at all possible to shorten it, and if it caught your interest but you need more info, here is the whole source for compiling (U762).
IS that a game, a program, or 3 year old drawing.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Jan 2016 10:58 Edited at: 4th Jan 2016 11:10
One thing I always do is make a colour type, and apply that to globals, types and other arrays as I need. Like for example:

Type _RGB
R as integer
G as integer
B as integer
A as integer
C as integer
Endtype

A is for Alpha, and C is for the calculated RGB value, which you might as well calculate when a colour is set or changed, rather than using RGB the whole time. It depends what you're using it for, but I always add them because they can be useful.

If you want to add the colour to an existing type:

Type _Objects
RGB as _RGB
X as float
etc
etc
etc
Endtype
Dim Obj(100) as _Objects

Now, you'd have Obj(0).RGB.R for red for example.

Say you wanted to keep a global variable for the sky colour:

Global Sky as _RGB
Sky.R=200 : Sky.G=170 : Sky.B=255 : Sky.A=128 : Sky.C=RGB(Sky.R,Sky.G,Sky.B)
Set Backdrop Color Sky.C

You want to blend between colours and stuff like that, so I'd just have an array for Sky - with maybe 5 indices, 0 being the current, and 1 to 5 being stored colours...

Dim Sky(5) as _RGB

To mix between, maybe something like this, setting the colour indice in an interpolation function:

Function SkyBlend(ColA,ColB,a#)
b#=1.0-a#
Sky(0).R=(Sky(ColA).R*a#)+(Sky(ColB).R*b#)
Sky(0).G=(Sky(ColA).G*a#)+(Sky(ColB).G*b#)
Sky(0).B=(Sky(ColA).B*a#)+(Sky(ColB).B*b#)
Sky(0).C=RGB(Sky(0).R,Sky(0).G,Sky(0).B)
Endfunction

So you'd store the sky colours in the Sky array, then use the SkyBlend function, passing say, (1,2,0.5) would mix colours 1 and 2 at 50%, storing the colour in Sky(0).C

You could even have a stored colour pallete, which is handy when you have a lot of user customisation. You could make a similar function to return the RGB value based on 2 stored palette values and a percentage... but anyway I'm sure that you can make your code far neater and more efficient with this approach.
The main thing I think is just to try out using a type for it, I think it'll make your code far more manageable and readable. Apologies for the missing code caps - they don't seem to be working for me so I'm just avoiding them for the time being.
Lukas W
21
Years of Service
User Offline
Joined: 5th Sep 2003
Location: Sweden
Posted: 4th Jan 2016 17:46 Edited at: 4th Jan 2016 17:48
I appreciate your tips. But for now, I just wanted to see if it was possible to shorten it.. Using your function, and ripping it out appears to have done the trick.

Quote: " If time.st_p >0
red# = (RGBR(sky_color(time.stage))*time.st_p)+(RGBR(sky_color(time.stagp))*(1.0-time.st_p))
gre# = (RGBG(sky_color(time.stage))*time.st_p)+(RGBG(sky_color(time.stagp))*(1.0-time.st_p))
blu# = (RGBB(sky_color(time.stage))*time.st_p)+(RGBB(sky_color(time.stagp))*(1.0-time.st_p))
Set Sprite Diffuse sky, red#, gre#, blu#
EndIf
Paste Sprite sky, 0, 0"


Edit,
It yields the same results as the 20lines from my original post, which is awesome.
IS that a game, a program, or 3 year old drawing.

Login to post a reply

Server time is: 2025-08-08 17:18:29
Your offset time is: 2025-08-08 17:18:29