Java Code Examples for java.awt.Color#getRGBComponents()

The following examples show how to use java.awt.Color#getRGBComponents() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: opt4j   File: Query.java    License: MIT License 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
	// Read the current color from the text field.
	String spec = getSelectedColor().trim();
	Color newColor = JColorChooser.showDialog(Query.this, "Choose Color", stringToColor(spec));

	if (newColor != null) {
		float[] components = newColor.getRGBComponents(null);
		StringBuffer string = new StringBuffer("{");

		// Use the syntax of arrays.
		for (int j = 0; j < components.length; j++) {
			string.append(components[j]);

			if (j < (components.length - 1)) {
				string.append(",");
			} else {
				string.append("}");
			}
		}

		_entryBox.setText(string.toString());
		_notifyListeners(_name);
	}
}
 
Example 2
private static boolean sameColor(Color c1, Color c2) {
    final float delta = 0.1f;

    float[] rgb1 = new float[4];
    float[] rgb2 = new float[4];

    rgb1 = c1.getRGBComponents(rgb1);
    rgb2 = c2.getRGBComponents(rgb2);

    for (int i = 0; i < rgb1.length; i++) {
        if (Math.abs(rgb1[i] - rgb2[i]) > delta) {
            return false;
        }
    }
    return true;
}
 
Example 3
Source Project: osp   File: DrawingPanel3D.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Computes the display color of a given drawable3D based on its original color and its depth.
 * Transparency of the original color is not affected.
 * @param _aColor the original color
 * @param _depth the depth value of the color
 */
Color projectColor(Color _aColor, double _depth) {
  if(!visHints.isUseColorDepth()) {
    return _aColor;
  }
  // if      (_depth<0.9) return _aColor.brighter().brighter();
  // else if (_depth>1.1) return _aColor.darker().darker();
  // else return _aColor;
  float[] crc = new float[4]; // Stands for ColorRGBComponent
  try {
    _aColor.getRGBComponents(crc);
    // Do not affect transparency
    for(int i = 0; i<3; i++) {
      crc[i] /= _depth;
      crc[i] = (float) Math.max(Math.min(crc[i], 1.0), 0.0);
    }
    return new Color(crc[0], crc[1], crc[2], crc[3]);
  } catch(Exception _exc) {
    return _aColor;
  }
}
 
Example 4
/**
 * Create a ColorValue from an existing Java Color.
 *
 * @param name The name of the color.
 * @param color An existing java color.
 */
private ConstellationColor(final String name, final Color color) {
    this(name, color.getRGBComponents(null)[0],
            color.getRGBComponents(null)[1],
            color.getRGBComponents(null)[2],
            color.getRGBComponents(null)[3]);
}
 
Example 5
public static void ChangeUserSelectedNodesColor(Vector<OneComponentNode> nodes, 
        Color newColor) {
    // Cycle through ComponentNode(s) and apply color
    float[] newColorComponentsAsFloatArray = newColor.getRGBComponents(null);
    double[] newColorComponentsAsDoubleArray = {newColorComponentsAsFloatArray[0], newColorComponentsAsFloatArray[1], newColorComponentsAsFloatArray[2]};
    for (OneComponentNode nextNode:nodes){
        ObjectDisplayColorAction.applyOperationToNode(nextNode, newColorComponentsAsDoubleArray);
    }
    
}
 
Example 6
protected Color interpolateColor(float t, Color start, Color end) {
    float[] partsS = start.getRGBComponents(null);
    float[] partsE = end.getRGBComponents(null);
    float[] partsR = new float[4];
    for (int i = 0; i < 4; i++) {
        partsR[i] = (partsS[i] - partsE[i]) * t + partsE[i];
    }
    return new Color(partsR[0], partsR[1], partsR[2], partsR[3]);
}
 
Example 7
public void setColor(Color color) {
    float[] rgb = color.getRGBComponents(null);
    this.colorRGB.val(0, rgb[0]*255);
    this.colorRGB.val(1, rgb[1]*255);
    this.colorRGB.val(2, rgb[2]*255);
    this.colorBGR.val(0, rgb[2]*255);
    this.colorBGR.val(1, rgb[1]*255);
    this.colorBGR.val(2, rgb[0]*255);
}
 
Example 8
public void actionPerformed(ActionEvent e) {
    // Read the current color from the text field.
    String spec = getSelectedColor().trim();
    String[] specArray = spec.split("[{},]");
    float red = 0f;
    float green = 0f;
    float blue = 0f;
    float alpha = 1.0f;
    // If any exceptions occur during the attempt to parse,
    // then just use the default color.
    try {
        int i = 0;
        // Ignore any blank strings that this simple parsing produces.
        while (specArray[i].trim().equals("")) {
            i++;
        }
        if (specArray.length > i) {
            red = Float.parseFloat(specArray[i]);
        }
        i++;
        while (specArray[i].trim().equals("")) {
            i++;
        }
        if (specArray.length > i) {
            green = Float.parseFloat(specArray[i]);
        }
        i++;
        while (specArray[i].trim().equals("")) {
            i++;
        }
        if (specArray.length > i) {
            blue = Float.parseFloat(specArray[i]);
        }
        i++;
        while (specArray[i].trim().equals("")) {
            i++;
        }
        if (specArray.length > i) {
            alpha = Float.parseFloat(specArray[i]);
        }
    } catch (Exception ex) {
        // Ignore and use default color.
    }
    Color newColor =
        JColorChooser.showDialog(
            Query.this,
            "Choose Background Color",
            new Color(red, green, blue, alpha));
    if (newColor != null) {
        float[] components = newColor.getRGBComponents(null);
        StringBuffer string = new StringBuffer("{");
        // Use the syntax of arrays.
        for (int j = 0; j < components.length; j++) {
            string.append(components[j]);
            if (j < components.length - 1) {
                string.append(",");
            } else {
                string.append("}");
            }
        }
        _entryBox.setText(string.toString());
        _notifyListeners(_name);
    }
}
 
Example 9
/**
 * Creates a new color with the hue taken from color <code>c</code>, but adjusted
 * in brightness to match the desired perceived brightness.
 * @param c
 * @param perceivedBrightness
 * @return
 */
public static Color createColor(Color c, float perceivedBrightness) {
	float[] cc = c.getRGBComponents(null);

	float pb = perceivedBrightness(c);
	if (pb == 0f)
		return new Color(pb, pb, pb, cc[3]);

	float f = perceivedBrightness / perceivedBrightness(c);
	float surplusBrightness = 0f;
	float sum = 0f;
	for (int i=0; i<3; i++) {
		cc[i] *= f;
		if (cc[i] < 1f) {
			sum += PERCEIVED_BRIGHTNESS[i];
			}
		else {
			surplusBrightness += (cc[i] - 1f) * PERCEIVED_BRIGHTNESS[i];
			cc[i] = 1f;
			}
		}
	if (surplusBrightness != 0) {
		float remainingBrightness = 0f;
		for (int i=0; i<3; i++) {
			if (cc[i] < 1f) {
				cc[i] += surplusBrightness / sum;
				if (cc[i] > 1f) {
					remainingBrightness += (cc[i] - 1f) * PERCEIVED_BRIGHTNESS[i];
					cc[i] = 1f;
					}
				}
			}
		if (remainingBrightness != 0f) {
			for (int i=0; i<3; i++) {
				if (cc[i] < 1f) {
					cc[i] += remainingBrightness / PERCEIVED_BRIGHTNESS[i];
					if (cc[i] > 1f) {
						cc[i] = 1f;
					}
				}
			}
			}
		}

	return new Color(cc[0], cc[1], cc[2], cc[3]);
	}
 
Example 10
/**
 * Set ambient
 *
 * @param value Color
 */
public void setAmbient(Color value) {
    this.ambient = value.getRGBComponents(null);
}
 
Example 11
/**
 * Set diffuse
 *
 * @param value Color
 */
public void setDiffuse(Color value) {
    this.diffuse = value.getRGBComponents(null);
}
 
Example 12
/**
 * Set specular
 *
 * @param value Color
 */
public void setSpecular(Color value) {
    this.specular = value.getRGBComponents(null);
}