Introduction
In this lab you will be learning about different types of colorblindness. The three types of colorblindness that we are addressing in this lab are protanopia, deuteranopia, and tritanopia. People with protanopia are unable to perceive ‘red’ light. People with deuteranopia are unable to perceive ‘green’ light. People with tritanopia are unable to perceive ‘blue’ light. All of these are oversimplifications of how colorblindness really works. This lab might suggest that a certain color palette may not work for a type of colorblindness when that is not really the case.
If you are curious about the different types of colorblindness you can click here
The reason we are talking about colorblindness in this lab is because it is important to be aware of the colors you are using when developing something such as a website. This is when inclusive design comes into play. When developing a website for example you want to make sure to utilize specific colors in order to make sure the website’s visitors will be able understand all of the content.
If you are curious about hexadecimal, read this section. Otherwise, you can jump to step 1. If this is confusing, don’t worry too much about the values actually being stored by hexadecimal, but it can be helpful to have a grasp of alternate numbering schemes. In this lab, we will be utilizing hexadecimal to represent colors. A color can be represented using three values: red, green, and blue. Each of these are typically between 0 and 255, inclusive. A single hexadecimal character can represent a number 0-15, much like how a single decimal character can represent a number 0-9. Counting in hexadecimal looks like
0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B … FD FE FF
FF is 255, the max value we will be using in this lab. Because each color (red, green, and blue) can be represented using two hexadecimal characters, a combination of these can be represented using six hexadecimal characters. For example, “FFFFFF” is white, as all three values are maxed at 255. You can experiment with creating colors using hexadecimal here.
Step 1 - Understanding Class Variables
Before you start writing any methods, look at the variable at the top of the class. These will represent whether the given color palette works for each type of color blindness. You will be able to access each of these in any of your methods.
Step 2 - Reading getRed
We have written getRed for you, so you have an example. Please read through it!
This method you will extract the red component from a String. This is a helper method and will come in handy later.
For more help on how Integer.parseInt() works, click here
Testing
You should go ahead and write some tests in runTests() to make sure getRed() is written correctly. This will give you practice before moving on.
Step 3 - Writing getGreen
This method is very similar to the one you wrote above however it is extracting the green component from a String. This is a helper method and will come in handy later.
You must first write the method signature. This method is a public method that returns an int. It is called getGreen and has one parameter that is a String that represents the hex you are extracting the color from.
Inside the method body you must first take a substring of the String parameter from 2-4. This gives us the third and fourth characters of the hex string, which represent the green component. Once you have done that you will return the substring parsed to an int. You can do this by using Integer.parseInt() and passing in the substring as the first parameter and 16 as the second.
Testing
You should write some tests in runTests for getGreen(). Maybe use the color picker. When you select a color, the second item in the RGB value is the green value! So using hex, #4d6285 the getGreen should return 98.
Step 4 - Writing getBlue
This method you will extract the blue component from a String. This is a helper method and will come in handy later.
You must first write the method signature. This method is a public method that returns an int. It is called getBlue and has one parameter that is a String that represents the hex you are extracting the color from.
Inside the method body you must first take a substring of the String parameter from 4 to the end of the String. This gives us the last two characters of the hex string, which represent the blue component. Once you have done that you will return the substring parsed to an int. You can do this by using Integer.parseInt() and passing in the substring as the first parameter and 16 as the second.
Testing
You should write some tests in runTests for getBlue().
Step 5 - Writing idDeuteranopia
This method returns true or false depending on whether or not if the given color is unaffected by Deuteranopia.
You must first write the method signature, it is a public method that returns a boolean. It is called idDeuteranopia and has one parameter that is a String and represents a color.
In the method body we are going to use one of the helper methods we just wrote. First create a variable of type int and assign it to the output of getGreen when passing in the String parameter. Then return the result of checking if this is equal to 0.
Testing
When writing tests for this method (aren’t you glad you wrote tests for getGreen?), make sure you pass in values where green if 00 or some other value!
Step 6 - Writing idProtanopia
This method returns true or false depending on whether or not if the given color is unaffected by Protanopia.
This method is a public method that returns a boolean. It is called idProtanopia and has one parameter that is a String that represents a color.
This method is very similar to the one you just wrote. You must first create a variable of type int and assign it to the output of getRed when passing in the String parameter. Then return the result of checking if this is equal to 0.
Testing
Always add tests for every method after you write them!
Step 7 - Writing idTritanopia
This method returns true or false depending on whether or not if the given color is unaffected by Tritanopia.
This method is a public method that returns a boolean. It is called idTritanopia and has one parameter that is a String that represents a color.
In this method you will create three int variables and initialize each to the output of getBlue, getRed, and getGreen when passing in the String parameter.
We will only return false from this method if several conditions are met. You can choose to write these as nested if statements or by combining conditions using the && operator. The three conditions we are looking to satisfy are:
- The blue component is greater than zero
- The red component is greater than 230
- The green component is greater than 220
If all of these conditions are met, return false. Otherwise, return true.
Testing
Always add tests for every method after you write them! This one has a range of values to test, so you probably need at least three to five tests to make sure it is correct.
Step 8 - Writing identifyPalette
This method takes a series of colors and sets the class variable to false if the palette does not work for that type of colorblindness.
This method is a public method that does not return anything. It is called identifyPalette and takes in one parameter that is a String that represents a color palette.
The color palette will be in the form of several six-character colors separated by spaces. For example, “FFFFFF FFFFFF” is a valid input. This method will iterate through each of the colors and determine if any of the colors make the color palette unsuitable for each type of colorblindness.
You must first create a for loop with a loop variable that starts at 0. This loop variable will be used as the starting index of each substring. We will continue looping while our loop variable is less than or equal to the length of the String parameter minus 6. The loop variable will be incremented by 7 between each iteration.
Inside the for loop you will declare a String and initialize it to a substring of the parameter String from your loop variable to the loop variable plus 6. This gives us a string representing one color.
Each of the class variables, protanopia, deuteranopia, and tritanopia, represent whether the color palette is suitable for each type of colorblindness so far. At the start of the program, all of these will be true, and we will set each to false if we find that a color does not work for that type of colorblindness.
We will write three if statements that check if each of our id methods return false when the color is passed in. You can use the not (!) operator to check if what is returned is false. The body of each of these if statements should set the corresponding class variable to false.
Testing
You will actually test this one after Step 9. By passing in inputs to the program, this method runs.
Step 9 - Finishing Main
Once you have completed your program go down to the main method and uncomment the line of code calling identifyPalette. Then you can test your program using the Scanner. You can input values into the terminal.
Step 10 - Turning in
Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get five submission attempts, so make sure it is mostly working before you submit. Make sure to comment out the runTests() line in your main before submitting!