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.

AppGameKit Classic Chat / making an rgb Darker vs Lighter , Code Inside. please help.

Author
Message
haliop
User Banned
Posted: 30th Jun 2013 13:39
hi again ..
im trying to set a RGB color Darker or Lighter while maintaining the color... so if i have a red color i want it to be dark red or bright red in real time..

i searched the web for HSL convertor but then realised i dont need it so i just searched for the simple how to make a RGB color lighter.. and found a simple 10 lines of code.

ive recreated it and got this:



if i Darken the color it works great and you can see it in the demo its pretty neat.
but if i Brighter the color its not 100 percent accurate since it sometimes turn the base color into something else while it trying to get to a full white color.. and that is not good with my app i need it 100 percent.. so theres probably a bit of problem with the code here.... im not a math man... any help will be appreciated.
thank you for your time.

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 30th Jun 2013 13:58
probably a typo, but you set P# in the function;

P#=sqrt((R11)*(R11)*Pr+(G11)*(G11)*Pg+(B11)*(B11)*Pb )

but use P to change the values.

R11=P+((R11)-P)*change

so effectively you are getting;

R11 = R11 * change
haliop
User Banned
Posted: 30th Jun 2013 14:06
haliop
User Banned
Posted: 30th Jun 2013 14:11
removed the P as it has no real effect whatsoever...



and yet still the Darken is working flawlessly but the Brighter not so good.

im probably missing a very small something...

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 30th Jun 2013 14:21
I think P# is important - I'm just not sure what it is doing.

It looks to be determining an average of R,G & B with biases based on Pr,Pg & Pb.

It then removes this average from the colour values before applying the change and then re-adds it after.

Try putting P# in the colour changes.

R11=P#+((R11)-P#)*change
haliop
User Banned
Posted: 30th Jun 2013 14:25
i did it was kinda messed up
however i do remmber when working on textures
when you lighten a color... lets say Red

(since darken is working good)

when you lighten a color... lets say Red

R = 255
g = 0
b = 0

only the G and B are gaining power , the Red Remains the same..
so maybe i should keep the Highest and change all others
however then P# comes in handy
becouse if i have for example

R = 255
G = 128
B = 0

it might cause a problem.. dont really know.. need to check
if anyone have anything to say please..

haliop
User Banned
Posted: 30th Jun 2013 14:26
well atleast P is not needed for Darkening since all the colors should lose power towards 0 0 0 = black

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 30th Jun 2013 14:34
I don't want to look at any math, unless I have written it myself I don't understand it

Anyway. If you want to darken a color you take the RGB values and lower each by a percentage. To lighten you need to do the same but in reverse. Basically, treat 255 as 0. The farther a color is from 255 the more you add.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Jun 2013 15:17
I think the problem is that you can't lighten a sprite. A sprite is it's normal colour at 255,255,255. You might be better converting it's image to a memblock and change each pixel but it won't be fast.


this.mess = abs(sin(times#))
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 30th Jun 2013 15:39
Bax is correct. If you are using this to alter the color of a sprite then you can't make it any brighter than it already is. For that you would have to put another sprite on top that has the same alpha but all colors are white. Then put that sprite on top of the regular one and alter it's transparency. So, if you want to make the color sprite brighter, you make the white sprite less transparent. You can use the same technique if you want to fade a sprite to a specific color, by altering the RGBA values of the front sprite.

BTW, if would be awesome if AppGameKit supported raising the RGB values above 255. But I guess you can do something similar with shaders in v2?

haliop
User Banned
Posted: 30th Jun 2013 15:48
the sprite in the code is just for testing... and it works.. so..
but in my app i dont use it with any sprites.. just want to get the correct RGB values...


for darken as written above its ok just take all the RGB and lower their values

but with lighten it is a problem cause some colors may have
r = 255
g = 128
b = 0

now if i'll do as you said
r = 255 cant go any upper then that
g will gain power until reaching 255
and also b
but in the time g and b goes up to 255 it will change into a diffrent color rather then saving the base color and just make it brighter...

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 30th Jun 2013 20:33 Edited at: 30th Jun 2013 20:33


This image is a proof of concept I made in Photoshop, using my suggested method for color manipulation (not sprite manipulation). I used your suggested color 255,128,0 and the hue stayed at exactly 30 on all colors.

Attachments

Login to view attachments
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 30th Jun 2013 21:26
Quote: "BTW, if would be awesome if AppGameKit supported raising the RGB values above 255."

This is not an AppGameKit limitation.

The limitation is in the 24 bit color system for many image formats like jpeg (which is not specific to AppGameKit by any means).

24 bits allows exactly 8 bits per color. And an unsigned 8 bit value allows only zero to 255.

While there are a lot of video cards and such that support 48 bit color (16 bit per RGB), using that would mean each image would be twice the file size for the same visual resolution.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 30th Jun 2013 22:51
You could just use variables to track the component colour values, and if the variable>255 then just use 255 for the colour component.

This would remove the 255 limitation.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Jul 2013 01:29
Quote: "You could just use variables to track the component colour values, and if the variable>255 then just use 255 for the colour component."

Wouldn't be that simple for accurate colors. If 24 bit color goes up to 255 for each component, and 48 bit goes up to 65535, then technically 255 = 65535. You'd want to do a proportion to scale the values for the 16bit channels down to 8 bit.

Simplest way if you absolutely need to represent 48 bit (pointless really unless you have a very very nice monitor), then:

red8 = (red16 / 65535) * 255

or:

red16 = (red8 / 255) * 65535

haliop
User Banned
Posted: 1st Jul 2013 09:15
thank you for your help
Digital Awekning what method did you suggested?

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Jul 2013 12:09
Simply treat 255 as 0 when brightening a sprite. The further away from 255 the more you change the color. In your example green is half the way from 255 compared to blue. So if you add 64 to blue, add 32 to green. This ensure a constant color.

Would be sweet if AppGameKit had a brightness setting for sprites. 0 is neutral, positive numbers makes it whiter and negative numbers adds black.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jul 2013 12:17
Quote: "Would be sweet if AppGameKit had a brightness setting for sprites. 0 is neutral, positive numbers makes it whiter and negative numbers adds black."

There are other ways to fake this (although not ideal). You can create a copy of your sprite that is pure white (in code or in photoshop), if you need to darken just leave this sprite transparent but if you need to lighten it just adjust the transparency of the "extra" sprite to become more visible towards 100% brightened. You would also have to position and rotate this extra sprite every loop over the top of your other sprite.

Alternatively you could wait for additive blending to be possible in AppGameKit probably in V2.


this.mess = abs(sin(times#))
haliop
User Banned
Posted: 1st Jul 2013 12:24
Digital can you write an example of what you mean?

credits will be given..inside the app.
i just dont get how to do it if the color is too complicated and i think you got it just right..

so if r is 255
g is 128
and b is 64...

hmmm man i really need to learn math!!!!

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jul 2013 13:20
If you're lightening it, you need to take the red DOWN from 255 as you bring the others up.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jul 2013 14:33
Try this version. It takes the current value and makes it a percentage lighter / darker than it's original colour:



this.mess = abs(sin(times#))
haliop
User Banned
Posted: 1st Jul 2013 15:38
very very COOL thank you.

ok , both of you please send me an email to this adress

nadav.rosenberg787@gmail.com

write your first and last name and your email if you wish 2
i will add you to the credit sectio of Nano.

Thank you alot , using it now

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jul 2013 15:48
No credit needed thanks


this.mess = abs(sin(times#))
haliop
User Banned
Posted: 1st Jul 2013 16:03
ok , i am more then willing to add credits of the ones who helped.
if you change your mind , please send me an email. thank you for your help.

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Jul 2013 17:13
Bax:
Check my previous post about altering sprite brightness
Thanks, now I don't have to think about how to code that thing

Jim:
That would turn down saturation and make the color grey.

Haliop:
No credits needed. It's such a small part of the overall code.

haliop
User Banned
Posted: 1st Jul 2013 17:24
its not how big it is vs the project
its the amount of time even if little that you helped me figure something out...

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jul 2013 19:16
No - the red has always to be higher, but less than maximum. The whiter it gets, the "pinker" it gets.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Jul 2013 20:34
Jim:
Not as long as blue is equal to green. That will keep hue to 0, full red. If you have more blue than green it become pink, more green than blue and it turns orange. If all 3 colors are below 255 the end result will start to become darker. When all 3 colors get closer to each other the end result will become more gray. You cannot make something 255 red brighter, without making it pale red.

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jul 2013 22:20
More white = less red. You can't simply ramp the other values up leaving red at 255.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Jul 2013 23:17
But if you lower red you get even less red than if you don't lower it. Again, it will turn darker and more gray as soon as you start to lower the red value. Start up Photoshop or something and experiment for yourself. PS got both HSB and RGB values in it's picker and it is very useful for these kinds of tests. And if you want to prove me wrong then post a picture of it. Like the one I did earlier.

Login to post a reply

Server time is: 2024-05-03 17:39:14
Your offset time is: 2024-05-03 17:39:14