Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Color object  (Read 2211 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans
« Posted 2014-06-10 23:31:02 »

I made this class a long time ago, and it still works. I thought you guy's may need a well-documented color object for whatever.
Feel free to credit me, or just remove the name. Its not like its protected or anything Wink

*Please just reply with any constructive criticism

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
package passage.blu.data;

import org.lwjgl.opengl.GL11;

/**
 * A class containing color specific data
 * like red, green, blue, and alpha transparency.
 *
 * @author Mitchell Hynes (Spacebeans)
 */

public class Color {
   public float r, g, b, a;
   
   /**
    * Constructs a color from red, green, blue, and alpha values.
    *
    * @param red The red hue
    * @param green The green hue
    * @param blue The blue hue
    * @param alpha The alpha transparency
    */

   public Color(float red, float green, float blue, float alpha){
      r = red;  g = green;  b = blue; a = alpha;
   }
   
   /**
    * Constructs a color from red, green, and blue values, with full alpha
    *
    * @param red The red hue
    * @param green The green hue
    * @param blue The blue hue
    */

   public Color(float red, float green, float blue){
      this(red, green, blue, 255);
   }
   
   /**
    * Sets the colors values
    *
    * @param r Red value (255)
    * @param g Green value (255)
    * @param b Blue value (255)
    * @param a Alpha value (255) - Transparency
    */

   public void set(float r, float g, float b, float a){
      this.r = r; this.g = g;  this.b = b; this.a = a;
   }
   
   /**
    * Gets all the values divided by the maximum color value.
    * (255)
    *
    * Mostly used in OpenGL bindings
    *
    * @return All the color values divided by the maximum color value.
    */

   public Color getAsFloat(){
      return new Color(r/255, g/255, b/255, a/255);
   }
   
   /**
    * Binds the color (glColor3f)
    */

    public void bind(){
      GL11.glColor4f(r, g, b, a);
    }
   
   /**
    * Resets OpenGL's current color to full
    * red, green, blue, and alpha.
     */

    public static void resetColors(){
      GL11.glColor4f(1, 1, 1, 1);
    }

   /**
    * @return The red color component
    */

   public float getRed() {
       return r;
    }

    /**
    * @param r The red component to set
     */

   public void setRed(float r) {
       this.r = r;
    }

   /**
    * @return The green color component
    */

   public float getGreen() {
       return g;
    }

   /**
    * @param g The green component to set
    */

    public void setGreen(float g) {
      this.g = g;
    }

   /**
    * @return The blue color component
    */

    public float getBlue() {
           return b;
   }

   /**
    * @param b The blue component to set
    */

    public void setBlue(float b) {
      this.b = b;
   }

   /**
    * @return The alpha component
    */

   public float getAlpha() {
      return a;
   }

   /**
    * @param a The alpha color component to set
    */

   public void setAlpha(float a) {
      this.a = a;
   }
}
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #1 - Posted 2014-06-11 00:12:18 »

Well, no offense, but what does your color class have to offer that something like Java's doesn't have?

"Binding" a color. You are using the old fixed function pipeline, something most people don't use anymore. Your bind method won't be as useful.

Its nice of you to offer up code, but really your color class is just a container for a couple of float values. If you want to make it useful add in some functions that the Java variant has such as brighten or darken. Anyone can write a container, what makes shared code useful is when someone writes the hard, intrictate functions.

Edit: You give me so many medals 0.0

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #2 - Posted 2014-06-11 00:14:36 »

If you are supplying setters (data is mutable) and there is no business logic in them, then why not just make the fields public?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Spacebeans
« Reply #3 - Posted 2014-06-11 00:18:25 »

If you are supplying setters (data is mutable) and there is no business logic in them, then why not just make the fields public?

It was made for a game engine. So I wanted to make a convention, kinda.

"Binding" a color. You are using the old fixed function pipeline, something most people don't use anymore. Your bind method won't be as useful.

Also a part of the game engine's convention. "Bind and Unbind". But yeah, they don't apply to its VBO sprite batches so whats the point. I may add some methods to darken and lighten, thanks for the info!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #4 - Posted 2014-06-11 00:27:11 »

Well I mean OpenGL is state based... So no matter what your engine will have to rely on binding and unbinding attributes. You can't get around that. But I was just saying that using the fixed function pipeline isn't going to be too useful!

Its hard to make a universal "bind" method because there are so many ways to apply color to vertices with OpenGL depending upon which version you are using. If you want to write a "universal" color class just remove any external dependencies in your code. Binding is something that will be handled differently in each engine.

A good design style is to always have classes handle themselves. There's a technical word for it, but I forgot it at the moment. Always have your classes manage themselves so you can easily modify code without breaking code in other classes.

Sorry for the whole design rant!

Offline The Lion King
« Reply #5 - Posted 2014-06-11 00:34:41 »

If you are supplying setters (data is mutable) and there is no business logic in them, then why not just make the fields public?

I don't agree with this as, if he later decides to add logic, it would cause a lot of code re write.

For example, his code probably should check for out of bounds on sets, and if he made the fields public then realized this later, it would be a huge overhaul.

Public fields cause classes to tightly couple and for dynamic and flexible design for a larger scale project (like the game engine that he is working on) this can cause a lot of headaches for even the smallest of changes.

And even if he doesn't. Why break encapsulation. Even if he never adds any logic at least he leaves himself the ability easily do so. for the sacrifice of a couple lines of code and a couple extra letters here and there.

Obviously if this is for something smaller scale that's gonna be used once and thrown away breaking some encapsulation and rules here and there for the sake of time is alright. But i think a color class is a pretty re-usable and extensible.

"You have to want it more than you want to breath, then you will be successful"
Offline Spacebeans
« Reply #6 - Posted 2014-06-11 00:40:22 »

Well I mean OpenGL is state based... So no matter what your engine will have to rely on binding and unbinding attributes. You can't get around that. But I was just saying that using the fixed function pipeline isn't going to be too useful!

Every damn thread you tell me the fixed function pipeline sucks, I know how to use the kinda-non-depreciated one. (Well, OpenGL 1.2-1.5 that is... Smiley )

Its hard to make a universal "bind" method because there are so many ways to apply color to vertices with OpenGL depending upon which version you are using. If you want to write a "universal" color class just remove any external dependencies in your code. Binding is something that will be handled differently in each engine.

I think I'll just stick with manually setting it up through the VBOs color float buffer for now Wink

A good design style is to always have classes handle themselves. There's a technical word for it, but I forgot it at the moment. Always have your classes manage themselves so you can easily modify code without breaking code in other classes.

Yeah, this engine handles lights so easily, that its just getWorld().addLight(*lightObject*), then the world has a list of "entities" that it could light. Those entities have "materials" that feed the light shader uniforms on normal, specular, and texture maps. So this engine's specifically made to handle things like this on its own. Mainly for people who want good-looking games with no effort.

Also, the engine comes with its own asset-editor, a way to edit levels, add lights, and animate materials. With real-time view of what it would look like. (UI done with JavaFX in a separate client), also making it really easy for non-programmers to get in on stuff.


Sorry for talking so much about the engine, I have allot of ideas.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #7 - Posted 2014-06-11 00:58:07 »

Oh sorry about spamming your threads with my "hate" for the fixed function pipeline! I always recommend to move away from it, I never noticed I've told you more than once! Tongue Also 1.2 - 1.5 isn't at all the programmable pipeline... Its still fixed function pipeline. I believe most people consider 3.x and above to be the modern stuff.

Online SHC
« Reply #8 - Posted 2014-06-11 05:24:19 »

I use this very long Color class in my engine. http://pastebin.java-gaming.org/fb93a53669f

Offline quew8

JGO Coder


Medals: 33



« Reply #9 - Posted 2014-06-11 19:09:56 »

My Color class is called "Colour." This is because I am English (and I don't expect others to use my code) but it actually turns out to be very useful in terms of avoiding naming conflicts because everything in Java uses the American spellings.

Since we are just talking about colours.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Spacebeans
« Reply #10 - Posted 2014-06-11 20:01:31 »

Oh sorry about spamming your threads with my "hate" for the fixed function pipeline! I always recommend to move away from it, I never noticed I've told you more than once! Tongue Also 1.2 - 1.5 isn't at all the programmable pipeline... Its still fixed function pipeline. I believe most people consider 3.x and above to be the modern stuff.

Well is shaders a part of the fixed pipline? Because there's a version in for it called "150" and I'm pretty sure they're read like "1.5". I may be wrong, I am usually wrong almost all the time when it comes to OpenGL for some reason.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #11 - Posted 2014-06-11 20:10:28 »

While we're sharing, here's mine from a while ago:
http://pastebin.java-gaming.org/b93a3666f96

The whole swap[] biz is because SunGraphics2D checks for object identity on setColor(), so you can't re-use 1 color object, you have to have 2, swapping back and forth.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #12 - Posted 2014-06-11 21:39:53 »

Oh sorry about spamming your threads with my "hate" for the fixed function pipeline! I always recommend to move away from it, I never noticed I've told you more than once! Tongue Also 1.2 - 1.5 isn't at all the programmable pipeline... Its still fixed function pipeline. I believe most people consider 3.x and above to be the modern stuff.

Well is shaders a part of the fixed pipline? Because there's a version in for it called "150" and I'm pretty sure they're read like "1.5". I may be wrong, I am usually wrong almost all the time when it comes to OpenGL for some reason.
No, shaders are essentially what make up the programmable pipeline (modern OpenGL). Shaders are little programs that process vertex data (hence programmable pipeline). The fixed function pipeline did not allow you to create your own shaders. You were stuck with the built in lighting functions and the matrix stack.
The version number you are using corresponds to the GLSL version you want to use, not the OpenGL version. GLSL 1.5 is different than OpenGL 1.5.

Offline Spacebeans
« Reply #13 - Posted 2014-06-11 22:02:29 »

The version number you are using corresponds to the GLSL version you want to use, not the OpenGL version. GLSL 1.5 is different than OpenGL 1.5.

Ahh. Right, +1
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #14 - Posted 2014-06-13 02:30:26 »

A good design style is to always have classes handle themselves. There's a technical word for it, but I forgot it at the moment. Always have your classes manage themselves so you can easily modify code without breaking code in other classes.

A bit late and a bit off-topic (I think we were already off), but I think this is important, it's at least related to (if not the) Open-Closed Principal.
I try to apply it when I can, it tends to make life much easier after a project gets to a certain size. A downside however is frequent instances of overeager API engineering instead of just getting stuff done.
Offline NegativeZero

JGO Knight


Medals: 49
Projects: 2


Zero but not.


« Reply #15 - Posted 2014-06-14 09:56:10 »

I just created my own Colour class, thought it'd be good to post here. It can convert both HSV and HSL values into RGB.

For those not in the know, HSV and HSL are colour data formats that are made to be more understandable by humans, as RGB is hard for people to visualise. It's what Paint, Paint.net, GIMP, Photoshop, etc.. use for their colour pickers, so if you're in the process of making anything that'd need a colour picker (E.G. a game engine, level editor, RPG with the ability to customise yourself) you can use this code to save 30 mins of your life ^.^

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
public class Colour {
   private Vector3 rgb;
   private float alpha;
   
   public Colour(){
      rgb = new Vector3(1f, 1f, 1f);
   }
   
   public float getRed(){
      return rgb.x;
   }
   
   public float getGreen(){
      return rgb.y;
   }
   
   public float getBlue(){
      return rgb.z;
   }
   
   public float getAlpha(){
      return alpha;
   }
   
   public void setRed(float red){
      rgb.x = red;
   }
   
   public void setGreen(float green){
      rgb.y = green;
   }
   
   public void setBlue(float blue){
      rgb.z = blue;
   }
   
   public void setAplha(float alpha){
      this.alpha = alpha;
   }
   
   public void setHSL(float H, float S, float L){
      H /= 60;
      float C = (1 - Math.abs(2 * L - 1)) * S;
      float X = C * (1 - Math.abs(H % 2 - 1));
      Vector3 rgb = new Vector3(0, 0, 0);
      if(0 <= H && H <= 1){
         rgb = new Vector3(C, X, 0.0f);
      }
      else if(1 <= H && H <= 2){
         rgb = new Vector3(X, C, 0);
      }
      else if(2 <= H && H <= 3){
         rgb = new Vector3(0, C, X);
      }
      else if(3 <= H && H <= 4){
         rgb = new Vector3(0, X, C);
      }
      else if(4 <= H && H <= 5){
         rgb = new Vector3(X, 0, C);
      }
      else if(5 <= H && H <= 6){
         rgb = new Vector3(C, 0, X);
      }
      float m = L - (0.5f * C);
      this.rgb = rgb.add(m);
   }  
   
   public void setHSV(float H, float S, float V){
      H /= 60;
      float C = V * S;
      float X = C * (1 - Math.abs(H % 2 - 1));
      Vector3 rgb = new Vector3(0, 0, 0);
      if(0 <= H && H <= 1){
         rgb = new Vector3(C, X, 0);
      }
      else if(1 <= H && H <= 2){
         rgb = new Vector3(X, C, 0);
      }
      else if(2 <= H && H <= 3){
         rgb = new Vector3(0, C, X);
      }
      else if(3 <= H && H <= 4){
         rgb = new Vector3(0, X, C);
      }
      else if(4 <= H && H <= 5){
         rgb = new Vector3(X, 0, C);
      }
      else if(5 <= H && H <= 6){
         rgb = new Vector3(C, 0, X);
      }
      float m = V - C;
      this.rgb = rgb.add(m);
   }
}
Offline Roquen
« Reply #16 - Posted 2014-06-14 10:52:39 »

For beginners doing stuff like this is okay.  Once your a little bit of experience they should make your eyes bleed.
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (86 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (81 views)
2014-12-05 12:39:02

SHC (91 views)
2014-12-03 16:27:13

CopyableCougar4 (98 views)
2014-11-29 21:32:03

toopeicgaming1999 (157 views)
2014-11-26 15:22:04

toopeicgaming1999 (154 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!