public final class Gray extends Object implements Comparable<Gray>
gray value as double | gray value as int |
---|---|
0.0 | 0 |
1.0 / 255.0 | 1 |
2.0 / 255.0 | 2 |
3.0 / 255.0 | 3 |
... | ... |
254.0 / 255.0 | 254 |
1.0 | 255 |
A Gray
instance can be created using any double value
between 0.0 and 1.0; it is not required that there be an exact
match between the double gray value and an int gray value.
See toInt(double)
for details on how a double gray value is
converted to an int gray value.
Modifier and Type | Field and Description |
---|---|
static double |
MAX_DOUBLE_VALUE
The maximum double gray value.
|
static int |
MAX_INT_VALUE
The maximum int gray value.
|
static double |
MIN_DOUBLE_VALUE
The minimum double gray value.
|
static int |
MIN_INT_VALUE
The minimum int gray value.
|
Constructor and Description |
---|
Gray(double g)
Initializes the gray value of this object to
g . |
Gray(Gray other)
Initializes the gray value of this object by copying another
Gray object. |
Gray(int g)
Initializes the gray value of this object to
g . |
Modifier and Type | Method and Description |
---|---|
double |
asDouble()
Returns the gray value of this object as a double.
|
int |
asInt()
Returns the gray value of this object as an int.
|
int |
compareTo(Gray other)
Compares two
Gray instances by their gray values. |
boolean |
equals(Object obj)
Compares two
Gray instances for equality. |
static Gray |
fromRGB(Color c)
Creates a
Gray instance from a Color . |
static void |
main(String[] args) |
static double |
toDouble(int value)
Converts an integer gray value to its equivalent double gray value.
|
static int |
toInt(double value)
Converts a double gray value to its equivalent int gray value.
|
String |
toString()
Returns a string representation of this
Gray instance. |
public static final double MIN_DOUBLE_VALUE
public static final double MAX_DOUBLE_VALUE
public static final int MIN_INT_VALUE
public static final int MAX_INT_VALUE
public Gray(double g)
g
.
The value returned by asDouble()
will be equal to
g
:
double g = 0.12345; Gray gray = new Gray(g); double value = gray.asDouble(); // value is equal to g
g
- a gray valueIllegalArgumentException
- if g is less than 0.0 or greater than 1.0public Gray(int g)
g
.
The value returned by asInt()
will be equal to
g
:
int g = 128; Gray gray = new Gray(g); int value = gray.asInt(); // value is equal to g
g
- a gray value between 0 and 255IllegalArgumentException
- if g is less than 0 or greater than 255public Gray(Gray other)
Gray
object.other
- a Gray
object to copypublic int asInt()
toInt(double)
for details).
Gray gray = new Gray(192); int value = gray.asInt(); // value is equal to 192 gray = new Gray(1.0); value = gray.asInt(); // value is equal to 255
public double asDouble()
toDouble(int)
for details).
Gray gray = new Gray(0.5); double value = gray.asDouble(); // value is equal to 0.5 gray = new Gray(255); value = gray.asDouble(); // value is equal to 1.0
public static double toDouble(int value)
value
- a gray value between 0 and 255IllegalArgumentException
- if value is less than 0 or greater than 255public static int toInt(double value)
value
- a gray value between 0.0 and 1.0IllegalArgumentException
- if value is less than 0.0 or greater than 1.0public static Gray fromRGB(Color c)
Gray
instance from a Color
.
The gray value of a color is given by
0.299 × R + 0.587 × G + 0.114 × B
where R is the red value of the color,
G is the green value of the color,
B is the blue value of the color, and all of R,
G, and B are double values between 0.0 and 1.0.c
- a colorpublic String toString()
Gray
instance.
The string is simply the gray value of this instance.
Gray gray = new Gray(18); String s = gray.toString(); // s is equal to "18" gray = new Gray(0.125); s = gray.toString(); // s is equal to "0.125"
public int compareTo(Gray other)
Gray
instances by their gray values.
The instances are compared using their gray values represented
as double values (between 0.0 and 1.0). Returns:-1
if this gray value is less than the other gray value,1
if this gray value is greater than the other gray value, and0
otherwise.compareTo
in interface Comparable<Gray>
public boolean equals(Object obj)
Gray
instances for equality.
The result is true
if and only if the argument is
not null
and is a Gray
instance whose
gray value is equal to this instance's gray value. The gray
levels are checked for equality by converting them to int
values (between 0 and 255) and comparing the two int values.public static void main(String[] args)