Package org.cis1200

Class Pixel

java.lang.Object
org.cis1200.Pixel
All Implemented Interfaces:
Comparable<Pixel>

public class Pixel extends Object implements Comparable<Pixel>
A point of color.

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

    Fields
    Modifier and Type
    Field
    Description
    static final Pixel
    The Pixel representing the RGB color black.
    static final Pixel
    The Pixel representing the RGB color blue.
    static final Pixel
    The Pixel representing the RGB color green.
    static final Pixel
    The Pixel representing the RGB color red.
    static final Pixel
    The Pixel representing the RGB color white.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pixel(int[] c)
    Create a new pixel with the provided color components, specified as an array.
    Pixel(int r, int g, int b)
    Create a new pixel with the provided color components.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    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
    equals(Object other)
    Checks whether this pixel has the same components as the given Object.
    int
    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
    Accessor for the green component of the pixel.
    int
    Accessor for the red component of the pixel.
    int
     
    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)
    Returns a string representation of this pixel.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • BLACK

      public static final Pixel BLACK
      The Pixel representing the RGB color black.
    • BLUE

      public static final Pixel BLUE
      The Pixel representing the RGB color blue.
    • RED

      public static final Pixel RED
      The Pixel representing the RGB color red.
    • GREEN

      public static final Pixel GREEN
      The Pixel representing the RGB color green.
    • WHITE

      public static final Pixel WHITE
      The Pixel 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 pixel
      g - the green component of the pixel
      b - 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 index c[0] corresponds to Pixel's red component; c[1] its green component, and c[2] its blue component.

      If c is null or has fewer than 3 entries, the missing components are set to 0. If c 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

      public int distance(Pixel px)
      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: use Math.abs
      Parameters:
      px - the other pixel with which to compare
      Returns:
      the sum of the differences in each of the color components
    • toString

      public String 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.

      Overrides:
      toString in class Object
      Returns:
      a string representation of this pixel
    • sameRGB

      public boolean sameRGB(Pixel px)
      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

      public boolean equals(Object other)
      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.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Pixel o)
      Specified by:
      compareTo in interface Comparable<Pixel>