Android Open Source - droidengine2d Color






From Project

Back to project page droidengine2d.

License

The source code is released under:

Apache License

If you think the Android project droidengine2d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*   Copyright 2013-2014 Miguel Vicente Linares
 *//w w  w .ja v a 2s . com
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.miviclin.droidengine2d.graphics;

/**
 * Color.
 * 
 * @author Miguel Vicente Linares
 * 
 */
public class Color {

  private float r;
  private float g;
  private float b;
  private float a;

  private float h;
  private float s;
  private float v;

  /**
   * Creates a new Color with the specified RGB values (values between 0.0f and 1.0f). The A (alpha) component will
   * have tha value 1.0f by default.
   * 
   * @param r Red.
   * @param g Green.
   * @param b Blue.
   */
  public Color(float r, float g, float b) {
    this(r, g, b, 1.0f);
  }

  /**
   * Creates a new Color with the specified RGBA values (values between 0.0f and 1.0f).
   * 
   * @param r Red.
   * @param g Green.
   * @param b Blue.
   * @param a Alpha.
   */
  public Color(float r, float g, float b, float a) {
    if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1 || a < 0 || a > 1) {
      throw new IllegalArgumentException("The RGB components must be values between 0.0 and 1.0");
    }
    this.r = r;
    this.g = g;
    this.b = b;
    this.a = a;
    convertRGBtoHSV();
  }

  /**
   * Creates a new Color, copying from the specified color.
   * 
   * @param color Color.
   */
  public Color(Color color) {
    this.r = color.r;
    this.g = color.g;
    this.b = color.b;
    this.a = color.a;
    this.h = color.h;
    this.s = color.s;
    this.v = color.v;
  }

  /**
   * Overwrites this Color with the specified one.<br>
   * This Color will be a copy of the specified Color.
   * 
   * @param color Color to copy.
   */
  public void set(Color color) {
    this.r = color.r;
    this.g = color.g;
    this.b = color.b;
    this.a = color.a;
    this.h = color.h;
    this.s = color.s;
    this.v = color.v;
  }

  /**
   * Sets the specified RGB values of this Color.
   * 
   * @param r Value between 0.0f and 1.0f.
   * @param g Value between 0.0f and 1.0f.
   * @param b Value between 0.0f and 1.0f.
   */
  public void setRGB(float r, float g, float b) {
    if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1) {
      throw new IllegalArgumentException("The RGB components must be values between 0.0 and 1.0");
    }
    this.r = r;
    this.g = g;
    this.b = b;
    convertRGBtoHSV();
  }

  /**
   * Sets the specified RGBA values of this Color.
   * 
   * @param r Value between 0.0f and 1.0f.
   * @param g Value between 0.0f and 1.0f.
   * @param b Value between 0.0f and 1.0f.
   * @param a Value between 0.0f and 1.0f.
   */
  public void setRGBA(float r, float g, float b, float a) {
    setRGB(r, g, b);
    setA(a);
  }

  /**
   * Updates the HSV values of this Color so they represent the same color that the RGB values represent.
   */
  private void convertRGBtoHSV() {
    float max = Math.max(Math.max(r, g), b);
    float min = Math.min(Math.min(r, g), b);
    float delta = max - min;
    float h = 0;
    float s = max == 0 ? 0 : (max - min) / max;
    float v = max;
    if (delta != 0) {
      if (r == max) {
        h = (g - b) / delta;
      } else if (g == max) {
        h = 2 + (b - r) / delta;
      } else {
        h = 4 + (r - g) / delta;
      }
      h *= 60;
      if (h < 0) {
        h += 360;
      }
    }
    this.h = h;
    this.s = s;
    this.v = v;
  }

  /**
   * Sets the specified HSV values of this Color.
   * 
   * @param h Value between 0.0f and 360.0f.
   * @param s Value between 0.0f and 1.0f.
   * @param v Value between 0.0f and 1.0f.
   */
  public void setHSV(float h, float s, float v) {
    if (h < 0 || h > 360) {
      throw new IllegalArgumentException("The H component must be a value between 0.0 and 360.0");
    }
    if (s < 0 || s > 1 || v < 0 || v > 1) {
      throw new IllegalArgumentException("The S and V components must be values between 0.0 and 1.0");
    }
    this.h = h;
    this.s = s;
    this.v = v;
    convertHSVtoRGB();
  }

  /**
   * Updates the RGB values of this Color so they represent the same color that the HSV values represent.
   */
  private void convertHSVtoRGB() {
    if (s == 0.0) {
      this.r = v;
      this.g = v;
      this.g = v;
      return;
    }
    float h = this.h;
    if (h == 360.0f) {
      h = 0.0f;
    }
    int hi = (int) (Math.floor(h / 60.0));
    float f = (h / 60.0f) - hi;
    float p = v * (1.0f - s);
    float q = v * (1.0f - (s * f));
    float t = v * (1.0f - (s * (1.0f - f)));
    switch (hi) {
    case 0:
      r = v;
      g = t;
      b = p;
      break;
    case 1:
      r = q;
      g = v;
      b = p;
      break;
    case 2:
      r = p;
      g = v;
      b = t;
      break;
    case 3:
      r = p;
      g = q;
      b = v;
      break;
    case 4:
      r = t;
      g = p;
      b = v;
      break;
    case 5:
      r = v;
      g = p;
      b = q;
      break;
    default:
      break;
    }

  }

  /**
   * Returns the R component (red) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getR() {
    return r;
  }

  /**
   * Returns the G component (green) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getG() {
    return g;
  }

  /**
   * Returns the B component (blue) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getB() {
    return b;
  }

  /**
   * Returns the A component (alpha) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getA() {
    return a;
  }

  /**
   * Sets the A component (alpha) of this Color to the specified value.
   * 
   * @param a Value between 0.0f and 1.0f
   */
  public void setA(float a) {
    if (a < 0 || a > 1) {
      throw new IllegalArgumentException("The alpha component must be a value between 0 and 1");
    }
    this.a = a;
  }

  /**
   * Returns the H component (hue) of this Color.
   * 
   * @return Value between 0.0f and 360.0f
   */
  public float getH() {
    return h;
  }

  /**
   * Returns the S component (saturation) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getS() {
    return s;
  }

  /**
   * Returns the V component (brightness) of this Color.
   * 
   * @return Value between 0.0f and 1.0f
   */
  public float getV() {
    return v;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Float.floatToIntBits(a);
    result = prime * result + Float.floatToIntBits(b);
    result = prime * result + Float.floatToIntBits(g);
    result = prime * result + Float.floatToIntBits(r);
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    Color other = (Color) obj;
    if (Float.floatToIntBits(a) != Float.floatToIntBits(other.a)) {
      return false;
    }
    if (Float.floatToIntBits(b) != Float.floatToIntBits(other.b)) {
      return false;
    }
    if (Float.floatToIntBits(g) != Float.floatToIntBits(other.g)) {
      return false;
    }
    if (Float.floatToIntBits(r) != Float.floatToIntBits(other.r)) {
      return false;
    }
    return true;
  }

}




Java Source Code List

com.miviclin.droidengine2d.AbstractGame.java
com.miviclin.droidengine2d.EngineActivity.java
com.miviclin.droidengine2d.EngineLock.java
com.miviclin.droidengine2d.Engine.java
com.miviclin.droidengine2d.GameThread.java
com.miviclin.droidengine2d.Game.java
com.miviclin.droidengine2d.audio.MusicPlayer.java
com.miviclin.droidengine2d.audio.SoundManager.java
com.miviclin.droidengine2d.gamestate.GameStateAdapter.java
com.miviclin.droidengine2d.gamestate.GameStateManager.java
com.miviclin.droidengine2d.gamestate.GameStateNotRegisteredException.java
com.miviclin.droidengine2d.gamestate.GameState.java
com.miviclin.droidengine2d.gamestate.OnGameStateChangeListener.java
com.miviclin.droidengine2d.graphics.Color.java
com.miviclin.droidengine2d.graphics.DefaultRenderer.java
com.miviclin.droidengine2d.graphics.EngineRenderer.java
com.miviclin.droidengine2d.graphics.GLDebugger.java
com.miviclin.droidengine2d.graphics.GLRenderer.java
com.miviclin.droidengine2d.graphics.GLView.java
com.miviclin.droidengine2d.graphics.Graphics.java
com.miviclin.droidengine2d.graphics.animation.AnimationFrame.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateAdapter.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateListener.java
com.miviclin.droidengine2d.graphics.animation.Animation.java
com.miviclin.droidengine2d.graphics.cameras.Camera.java
com.miviclin.droidengine2d.graphics.cameras.OrthographicCamera.java
com.miviclin.droidengine2d.graphics.material.BlendingOptions.java
com.miviclin.droidengine2d.graphics.material.ColorMaterial.java
com.miviclin.droidengine2d.graphics.material.Material.java
com.miviclin.droidengine2d.graphics.material.TextureColorMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureHsvMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureMaterial.java
com.miviclin.droidengine2d.graphics.material.TransparentTextureMaterial.java
com.miviclin.droidengine2d.graphics.material.UnsupportedMaterialException.java
com.miviclin.droidengine2d.graphics.mesh.ColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.Geometry.java
com.miviclin.droidengine2d.graphics.mesh.GraphicsBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchGeometry.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureHsvMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRendererBase.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TransparentTextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgramException.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgram.java
com.miviclin.droidengine2d.graphics.shader.ShaderVars.java
com.miviclin.droidengine2d.graphics.text.BitmapFont.java
com.miviclin.droidengine2d.graphics.text.FontChar.java
com.miviclin.droidengine2d.graphics.text.Font.java
com.miviclin.droidengine2d.graphics.text.UndefinedCharacterException.java
com.miviclin.droidengine2d.graphics.texture.TextureAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureManager.java
com.miviclin.droidengine2d.graphics.texture.TexturePackerAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureRegion.java
com.miviclin.droidengine2d.graphics.texture.Texture.java
com.miviclin.droidengine2d.input.DefaultKeyEventProcessor.java
com.miviclin.droidengine2d.input.GameInputManager.java
com.miviclin.droidengine2d.input.GameStateInputManager.java
com.miviclin.droidengine2d.input.KeyEventInfo.java
com.miviclin.droidengine2d.input.KeyEventProcessor.java
com.miviclin.droidengine2d.input.KeyProcessor.java
com.miviclin.droidengine2d.input.MotionEventProcessor.java
com.miviclin.droidengine2d.input.TouchProcessor.java
com.miviclin.droidengine2d.input.sensor.AccelerometerValuesListener.java
com.miviclin.droidengine2d.input.sensor.Accelerometer.java
com.miviclin.droidengine2d.input.sensor.SensorUtilities.java
com.miviclin.droidengine2d.resources.AssetsLoader.java
com.miviclin.droidengine2d.util.ActivityUtilities.java
com.miviclin.droidengine2d.util.MutexLock.java
com.miviclin.droidengine2d.util.PrimitiveTypeSize.java
com.miviclin.droidengine2d.util.TransformUtilities.java
com.miviclin.droidengine2d.util.Transform.java
com.miviclin.droidengine2d.util.math.Matrix4.java
com.miviclin.droidengine2d.util.math.MatrixFix.java
com.miviclin.droidengine2d.util.math.Vector2.java
com.miviclin.droidengine2d.util.math.Vector3.java
com.miviclin.droidengine2d.util.time.TimeConstants.java
com.miviclin.droidengine2d.util.time.TimeCounter.java