Color object

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

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;
	}
}

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

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.

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!

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!

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.

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… :slight_smile: )

I think I’ll just stick with manually setting it up through the VBOs color float buffer for now :wink:

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.

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! :stuck_out_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.

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

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.

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.

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.

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.

Ahh. Right, +1

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.

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 ^.^

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);
	}
}


For beginners doing stuff like this is okay. Once your a little bit of experience they should make your eyes bleed.