Color Palettes

Hello, I am a “new kid in the block” here.
I am impressed with the quality of tutorials, documentation and the active community, I am still discovering the engine , but so far it looks really awesome.

I started experimenting around, playing with some simple data visualizations (3d charts, I am just learning , so I stick to simple stuff :slight_smile: ) and started creating some handy (for me) static methods to deal with Palettes and materials.

Something like :

palette = Pal.createInterpolatedPalette(Pal.color(1f,1f,0f),Pal.color(1f,0.2f,0.2f),16);
palette2 = Pal.setPaletteSaturation(palette,1.5f);

Is there anything like this already somewhere in JME3?
If not, should I share my code (I am happy to if it can be useful to others)? How?

regards,

Francesco

I don’t know for sure if we have this already, but you basically can’t go wrong with sharing your code up front :wink:

Welcome to the community!

Ok then it’s just basic stuff so far, let me know if you want me to update it with whatever I will probably add in the next days… :slight_smile:

[java]
package com.jme3.util;

import com.jme3.math.ColorRGBA;

public class Pal {

  /**
   * 
   * @param rgb Internet color code RRGGBB
   * @return
   */
  public static ColorRGBA color(int rgb)
  {
	  return color( (rgb >>16) & 0xff, (rgb >>8) & 0xff,rgb  & 0xff );
  }
  
  /**
   * 
   * @param r
   * @param g
   * @param b
   * @param a
   * @return an RGBA color
   */
  public static ColorRGBA color(float r,float g, float b,float a)
  {
	  return new ColorRGBA(r,g,b,a);
  }
  
  /**
   * 
   * @param r
   * @param g
   * @param b
   * @return an RGBA color (with Alpha = 1)
   */
  public static ColorRGBA color(float r,float g, float b)
  {
	  return new ColorRGBA(r,g,b,1f);
  }
    
  /**
   * 
   * @param r
   * @param g
   * @param b
   * @param a
   * @return
   */
  public static ColorRGBA color(int r,int g, int b,int a)
  {
	  return new ColorRGBA(r/255f,g/255f,b/255f,a/255f);
  }
  
  /**
   * 
   * @param r
   * @param g
   * @param b
   * @return
   */
  public static ColorRGBA color(int r,int g, int b)
  {
	  return new ColorRGBA(r/255f,g/255f,b/255f,1f);
  }

  
  /**
   * Creates a linearly interpolated palette from startC to endC
   * @param startC
   * @param endC
   * @param elements
   * @return
   */
  public static ColorRGBA[] createInterpolatedPalette(ColorRGBA startC,ColorRGBA endC,int elements)
  {
	  ColorRGBA[] palette = new ColorRGBA[elements];
	  float dr = (endC.r-startC.r)/elements;
	  float dg = (endC.g-startC.g)/elements;
	  float db = (endC.b-startC.b)/elements;
	  float da = (endC.a-startC.a)/elements;
	  palette[0]=new ColorRGBA(startC);
	  palette[elements-1]=new ColorRGBA(endC);
	  for (int i=1;i<elements-1;i++)
		  palette[i] = color(startC.r + dr*i,startC.g + dg*i,startC.b + db*i,startC.a+da*i);
	  return palette;
  }
  
  /**
	 * I need a palette, quick!
	 * @return a 18 elements palette
	 */
	public static ColorRGBA[] createSamplePalette()
	{	
		ColorRGBA[] pal = new ColorRGBA[18];
		pal[0]  = color(0xFCE94F);	
		pal[1]  = color(0xFCAF3E);	
		pal[2]  = color(0xE9B96E);	
		pal[3]  = color(0x8AE234);	
		pal[4]  = color(0x729FCF);	
		pal[5]  = color(0xAD7FA8);	
		pal[6]  = color(0xEF2929);	
		pal[7]  = color(0xEEEEEC);	
		pal[8]  = color(0x888A85);
	    pal[9]  = color(0xC4A000);	
	    pal[10] = color(0xCE5C00);
	    pal[11] = color(0x8F5902);
	    pal[12] = color(0x4E9A06);
	    pal[13] = color(0x204A87);
	    pal[14] = color(0x5C3566);
	    pal[15] = color(0xA40000);
	    pal[16] = color(0xBABDB6);
	    pal[17] = color(0x2E3436);
		return pal;
	}
	
	/**
	 * Sets alpha values for a palette
	 * @param palette
	 * @param alpha
	 * @return
	 */
	public static ColorRGBA[]  setPaletteAlpha(ColorRGBA[] palette, float alpha)
	{
		ColorRGBA[] palette2 = new ColorRGBA[palette.length];
		for (int i=0;i<palette.length;i++)
		 palette2[i] = color(palette[i].r ,palette[i].g ,palette[i].b  ,alpha);
		return palette2;
	}
	
	/**
	 * Multiplies saturation for a given color
	 * @param col
	 * @param sat (>1 increases saturation , <1 decreases it)
	 * @return
	 */
	public static ColorRGBA setColorStaturation(ColorRGBA col, float sat)
	{
		float avg = (col.r +col.g + col.b) /3; // should I use geometric average instead?
		 // uhm probably this one is faster
		return color( (col.r - avg)*sat+col.r,
	             (col.g - avg)*sat+col.g , 
	             (col.b - avg)*sat+col.b,
	             col.a);
	}
	
	/**
	 * Multiplies luminosity (brightness) of a given color
	 * @param col
	 * @param lum (>1 increases luminosity , <1 decreases it)
	 * @return
	 */
	public static ColorRGBA setColorLumin(ColorRGBA col, float lum)
	{
		return col.mult(lum);//color(col.r *lum,col.g *lum,col.b *lum,col.a);	
	}
	
	/**
	 * Multiplies luminosity (brightness) of a given palette
	 * @param palette
	 * @param lum (>1 increases luminosity , <1 decreases it)
	 * @return
	 */
	public static ColorRGBA[]  setPaletteLumin(ColorRGBA[] palette, float lum)
	{
		ColorRGBA[] palette2 = new ColorRGBA[palette.length];
		for (int i=0;i<palette.length;i++)
			 palette2[i] = palette[i].mult(lum);// color(palette[i].r *lum,palette[i].g *lum,palette[i].b *lum,palette[i].a);
		return palette2;
	}
	
	
	/**
	 * Multiplies saturation for a given palette
	 * @param palette
	 * @param sat (>1 increases Saturation , <1 decreases it)
	 * @return
	 */
	public static ColorRGBA[]  setPaletteSaturation(ColorRGBA[] palette, float sat)
	{
		ColorRGBA[] palette2 = new ColorRGBA[palette.length];
		for (int i=0;i<palette.length;i++)
		{
		    float avg = (palette[i].r +palette[i].g + palette[i].b) /3;
			palette2[i] = color( (palette[i].r - avg)*sat+palette[i].r,
					             (palette[i].g - avg)*sat+palette[i].g , 
					             (palette[i].b - avg)*sat+palette[i].b 
					             ,palette[i].a);
		}
	    return palette2;
	}

}

[/java]

and a small demo class

[java]
package tests;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.util.Pal;

public class PaletteDemo extends SimpleApplication{

public void simpleInitApp() {

	// create some palettes
	ColorRGBA[][] palettes = new ColorRGBA[4][16];
	palettes[0] = Pal.createSamplePalette();
	palettes[1] = Pal.setPaletteSaturation(palettes[0],1.5f);
	palettes[2] = Pal.createInterpolatedPalette(Pal.color(1f,1f,0f),Pal.color(0.8f,0.1f,0.3f),16);
	palettes[3] = Pal.setPaletteLumin(palettes[2],0.6f);
	
	// create objects with colors from the different palettes
	for (int i=0;i<16;i++)
	 for (int j=0;j<4;j++)
	 {
	   Geometry g = new Geometry("box_"+i+"_"+j,new Box(0.4f,0.4f,0.4f)); // cube 0.8x0.8x0.8
	   Material m = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
	   g.setMaterial(m);
	   m.setColor("Color",palettes[j][i]);
	   g.setLocalTranslation(i-7.5f,j-1.5f,0f); 
	   rootNode.attachChild(g);	
	 }
}

/**
 * @param args
 */
public static void main(String[] args) {
   PaletteDemo pd = new PaletteDemo();
   pd.start();
}

}

[/java]

1 Like