Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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 453 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS
« Posted 2013-08-05 22: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?

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-08-05 23: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-05 23: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
« Reply #3 - Posted 2013-08-05 23: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
« Reply #4 - Posted 2013-08-05 23: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?

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-08-05 23: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
« Reply #6 - Posted 2013-08-05 23:11:20 »

Which Color class are you using? java.awt.Color ?
That would be correct

Offline davedes
« Reply #7 - Posted 2013-08-05 23:16:14 »

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

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2013-08-05 23: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
« Reply #9 - Posted 2013-08-05 23: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.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (51 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!