001/* 002 * Color4f -- Represents a Java3D Color4f tuple that can accept an AWT Color object. 003 * 004 * Copyright (C) 2023, by Joseph A. Huwaldt. 005 * All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 * Or visit: http://www.gnu.org/licenses/lgpl.html 021 */ 022package jahuwaldt.j3d; 023 024 025import java.awt.Color; 026import org.jogamp.vecmath.Tuple4d; 027import org.jogamp.vecmath.Tuple4f; 028 029 030/** 031 * A four-element color represented by single precision floating point 032 * x, y, z, and w values. The x, y, z, and w values represent the red, 033 * blue, green, and alpha color values, respectively. Color and alpha 034 * components should be in the range [0.0, 1.0]. 035 * <p> 036 * Java 3D assumes that a linear (gamma-corrected) visual is used for 037 * all colors. 038 * 039 */ 040public class JColor4f extends org.jogamp.vecmath.Color4f implements java.io.Serializable { 041 042 // Compatible with 1.1 043 static final long serialVersionUID = 1L; 044 045 /** 046 * Constructs and initializes a Color4f from the specified xyzw 047 * coordinates. 048 * @param x the red color value 049 * @param y the green color value 050 * @param z the blue color value 051 * @param w the alpha value 052 */ 053 public JColor4f(float x, float y, float z, float w) { 054 super(x,y,z,w); 055 } 056 057 058 /** 059 * Constructs and initializes a Color4f from the array of length 4. 060 * @param c the array of length 4 containing r,g,b,a in order 061 */ 062 public JColor4f(float[] c) { 063 super(c); 064 } 065 066 067 /** 068 * Constructs and initializes a Color4f from the specified Color4f. 069 * @param c1 the Color4f containing the initialization r,g,b,a data 070 */ 071 public JColor4f(JColor4f c1) { 072 super(c1); 073 } 074 075 076 /** 077 * Constructs and initializes a Color4f from the specified Tuple4f. 078 * @param t1 the Tuple4f containing the initialization r,g,b,a data 079 */ 080 public JColor4f(Tuple4f t1) { 081 super(t1); 082 } 083 084 085 /** 086 * Constructs and initializes a Color4f from the specified Tuple4d. 087 * @param t1 the Tuple4d containing the initialization r,g,b,a data 088 */ 089 public JColor4f(Tuple4d t1) { 090 super(t1); 091 } 092 093 094 /** 095 * Constructs and initializes a Color4f from the specified AWT 096 * Color object. 097 * No conversion is done on the color to compensate for 098 * gamma correction. 099 * 100 * @param color the AWT color with which to initialize this 101 * Color4f object 102 * 103 * @since vecmath 1.2 104 */ 105 public JColor4f(Color color) { 106 super((float)color.getRed() / 255.0f, 107 (float)color.getGreen() / 255.0f, 108 (float)color.getBlue() / 255.0f, 109 (float)color.getAlpha() / 255.0f); 110 } 111 112 113 /** 114 * Constructs and initializes a Color4f to (0.0, 0.0, 0.0, 0.0). 115 */ 116 public JColor4f() { 117 super(); 118 } 119 120 121 /** 122 * Sets the r,g,b,a values of this Color4f object to those of the 123 * specified AWT Color object. 124 * No conversion is done on the color to compensate for 125 * gamma correction. 126 * 127 * @param color the AWT color to copy into this Color4f object 128 * 129 * @since vecmath 1.2 130 */ 131 public final void set(Color color) { 132 x = (float)color.getRed() / 255.0f; 133 y = (float)color.getGreen() / 255.0f; 134 z = (float)color.getBlue() / 255.0f; 135 w = (float)color.getAlpha() / 255.0f; 136 } 137 138 139 /** 140 * Returns a new AWT color object initialized with the r,g,b,a 141 * values of this Color4f object. 142 * 143 * @return a new AWT Color object 144 * 145 * @since vecmath 1.2 146 */ 147 public final Color get() { 148 int r = Math.round(x * 255.0f); 149 int g = Math.round(y * 255.0f); 150 int b = Math.round(z * 255.0f); 151 int a = Math.round(w * 255.0f); 152 153 return new Color(r, g, b, a); 154 } 155 156}