Class Luminance
- Object
-
- Luminance
-
public class Luminance extends Object
The classLuminanceis a library of static methods related to the monochrome luminance of a color. It supports computing the monochrome luminance of a color (r, g, b) using the NTSC formula Y = 0.299*r + 0.587*g + 0.114*b; converting the color to a grayscale color, and checking whether two colors are compatible.For additional documentation, see Section 3.1 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description Luminance()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareCompatible(Color a, Color b)Are the two given colors compatible? Two colors are compatible if the difference in their monochrome luminances is at least 128.0).static doubleintensity(Color color)Returns the monochrome luminance of the given color as an intensity between 0.0 and 255.0 using the NTSC formula Y = 0.299*r + 0.587*g + 0.114*b.static voidmain(String[] args)Takes six command-line arguments r1, g1, b1, r2, g2, and b2, prints to standard output the monochrome luminances of (r1, g1, b1) and (r2, g2, b2) and whether they are compatible.static ColortoGray(Color color)Returns a grayscale version of the given color as aColorobject.
-
-
-
Method Detail
-
intensity
public static double intensity(Color color)
Returns the monochrome luminance of the given color as an intensity between 0.0 and 255.0 using the NTSC formula Y = 0.299*r + 0.587*g + 0.114*b. If the given color is a shade of gray (r = g = b), this method is guaranteed to return the exact grayscale value (an integer with no floating-point roundoff error).- Parameters:
color- the color to convert- Returns:
- the monochrome luminance (between 0.0 and 255.0)
-
toGray
public static Color toGray(Color color)
Returns a grayscale version of the given color as aColorobject.- Parameters:
color- theColorobject to convert to grayscale- Returns:
- a grayscale version of
color
-
areCompatible
public static boolean areCompatible(Color a, Color b)
Are the two given colors compatible? Two colors are compatible if the difference in their monochrome luminances is at least 128.0).- Parameters:
a- one colorb- the other color- Returns:
trueif colorsaandbare compatible;falseotherwise
-
main
public static void main(String[] args)
Takes six command-line arguments r1, g1, b1, r2, g2, and b2, prints to standard output the monochrome luminances of (r1, g1, b1) and (r2, g2, b2) and whether they are compatible.- Parameters:
args- the six command-line arguments
-
-