Class Pixel
- All Implemented Interfaces:
Comparable<Pixel>
Pixels are represented as three integral color components (red, green, and
blue) in the inclusive range [0, 255]. Lower values mean less color; higher
mean more. For example, new Pixel(255,255,255)
represents white,
new Pixel(0,0,0)
represents black, and new Pixel(0,255,0)
represents green.
This data structure is immutable. Once a Pixel
is created, it cannot
be modified.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Pixel
ThePixel
representing the RGB color black.static final Pixel
ThePixel
representing the RGB color blue.static final Pixel
ThePixel
representing the RGB color green.static final Pixel
ThePixel
representing the RGB color red.static final Pixel
ThePixel
representing the RGB color white. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
int
Determines the level of similarity between this pixel and another by summing the absolute values of the differences between corresponding components of the two pixels.boolean
Checks whether this pixel has the same components as the given Object.int
getBlue()
Accessor for the blue component of the pixel.int[]
Accessor for the pixel's components as an array of 3 integers, where index 0 is red, index 1 is green, and index 2 is blue.int
getGreen()
Accessor for the green component of the pixel.int
getRed()
Accessor for the red component of the pixel.int
hashCode()
boolean
Compares the RGB values of the current Pixel with another to check if they are the same (and thus whether the two Pixels equal each other)toString()
Returns a string representation of this pixel.
-
Field Details
-
BLACK
ThePixel
representing the RGB color black. -
BLUE
ThePixel
representing the RGB color blue. -
RED
ThePixel
representing the RGB color red. -
GREEN
ThePixel
representing the RGB color green. -
WHITE
ThePixel
representing the RGB color white.
-
-
Constructor Details
-
Pixel
public Pixel(int r, int g, int b) Create a new pixel with the provided color components.If the provided components are not between 0 and 255, they are clipped. Negative components are set to 0, and components greater than 255 are set to 255.
- Parameters:
r
- the red component of the pixelg
- the green component of the pixelb
- the blue component of the pixel
-
Pixel
public Pixel(int[] c) Create a new pixel with the provided color components, specified as an array. The indexc[0]
corresponds toPixel
's red component;c[1]
its green component, andc[2]
its blue component.If
c
is null or has fewer than 3 entries, the missing components are set to 0. Ifc
has more than 3 entries, the extra entries are ignored. If the provided components are not between 0 and 255, they are clipped. Negative components are set to 0, and components greater than 255 are set to 255.This constructor must not throw any exceptions.
- Parameters:
c
- the array of components
-
-
Method Details
-
getRed
public int getRed()Accessor for the red component of the pixel.- Returns:
- the int value of the red component
-
getGreen
public int getGreen()Accessor for the green component of the pixel.- Returns:
- the int value of the green component
-
getBlue
public int getBlue()Accessor for the blue component of the pixel.- Returns:
- the int value of the blue component
-
getComponents
public int[] getComponents()Accessor for the pixel's components as an array of 3 integers, where index 0 is red, index 1 is green, and index 2 is blue. Note that this method should not break encapsulation.- Returns:
- an int array representing the pixel's components
-
distance
Determines the level of similarity between this pixel and another by summing the absolute values of the differences between corresponding components of the two pixels. Distance to a null pixel is defined as -1. Hint: useMath.abs
- Parameters:
px
- the other pixel with which to compare- Returns:
- the sum of the differences in each of the color components
-
toString
Returns a string representation of this pixel. The string should comma separate the rgb values and surround them with parentheses.For example,
RED.toString()
is"(255, 0, 0)"
Note: This function will allow you to print pixels in a readable format. This can be very helpful while debugging, and we highly encourage you to use print statements to aid your debugging throughout this assignment.
-
sameRGB
Compares the RGB values of the current Pixel with another to check if they are the same (and thus whether the two Pixels equal each other)- Parameters:
px
- The pixel being compared with this- Returns:
- whether the two pixels contain the same components
-
equals
Checks whether this pixel has the same components as the given Object. If the other object is not a Pixel, then the method returns false. -
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<Pixel>
-