Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting a color in a 0-1 float value  (Read 400 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS

Junior Member


Medals: 1



« Posted 2013-08-06 00:50:08 »

Is there a method gives you the color's component's value in a float value in the range 0-1? It's odd I haven't found one since you can declare a color in this way (using the 0-1 float value that is).

In case there isn't one, I quickly wrote this code:
1  
2  
3  
double r = (double)(new Color(0.1F, 0.2F, 0.3F, 0.4F)).getRed();
r /= 256;
System.out.println(r);

This outputs 0.1015625 as the amount of red in the color but you can clearly tell it really is 0.1. Is there a more accurate way of doing this?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-08-06 01:03:10 »

If the Color class saves the values as bytes, then there will only be 256 possible values in each channel, which leads to the inaccuracy you're seeing.

Last but not least, you should divide by 255, not 256.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline davedes
« Reply #2 - Posted 2013-08-06 01:05:14 »

Generally you shouldn't rely on float precision. See here for a brief introduction:
http://www.saylor.org/site/wp-content/uploads/2011/05/floating-point.pdf

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VaPS

Junior Member


Medals: 1



« Reply #3 - Posted 2013-08-06 01:07:04 »

If the Color class saves the values as bytes, then there will only be 256 possible values in each channel, which leads to the inaccuracy you're seeing.

Last but not least, you should divide by 255, not 256.
With some confusion (I'm new!), thank you for your answer! So basically, there is no way to 100% accurately retrieve the number?
Offline VaPS

Junior Member


Medals: 1



« Reply #4 - Posted 2013-08-06 01:09:37 »

Generally you shouldn't rely on float precision. See here for a brief introduction:
http://www.saylor.org/site/wp-content/uploads/2011/05/floating-point.pdf
There is no constructor for double's in color unfortunately. Would you suggest using int's for putting RGB values in instead?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-08-06 01:10:30 »

Which Color class are you using? java.awt.Color ?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline VaPS

Junior Member


Medals: 1



« Reply #6 - Posted 2013-08-06 01:11:20 »

Which Color class are you using? java.awt.Color ?
That would be correct
Offline davedes
« Reply #7 - Posted 2013-08-06 01:16:14 »

Yes, just specify the R, G, B, A integers in the range 0 - 255 if you need an exact color.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2013-08-06 01:17:08 »

No matter the constructor, the java.awt.Color class stores each color channel (RGBA) in 8 bits, so that's 256 values. Given that floats have over 4 billion values - although a few less in the range 0..1 - you're bound to lose precision.

1  
2  
3  
4  
5  
      float input = 0.1f;
      int channel = (int) (input * 255.0f + 0.5f);
      float output = channel / 255.0f;

      System.out.println(output);

1  
0.101960786

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline VaPS

Junior Member


Medals: 1



« Reply #9 - Posted 2013-08-06 01:19:39 »

Thanks to both of you, I'm switching to using the 0-255 way of doing it!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (25 views)
2014-04-14 17:35:47

Porlus (42 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (223 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!