RGB to Hex

I think this was probably one of the toughest Reddit challenges I solved. However, it's worth noting this does not just convert hex numbers to RGB. It sort of blurs colors as well by combining and averaging values prior to the conversion it performs. There is quite a bit going on here so I'm glad I had the foresight to create multiple different functions. Without further ado, let's jump into how I convert RGB values to Hex values. The first step is to avoid over importing, and to set myself up for success with the traditional steps in Java development.

initialize the variables for the first three colors

Because of the structure the main function is a single line of code that executes the main function.

The main function is just a single line of code

The main RGBToHex function starts out by defining several scanners and variables to set ourselves up for success. From there, I once again ask the user to give me the number of red values they want to work with, and use that number to set the size of my static array. It's not very hard to do, and it gives better performance. After that I use a do... while loop. A do... while loop will always execute at least once, and after that first time keep executing if the condition for ending the loop has not been met. The same process continues for green and blue values.

Utilize do while loops when the code needs to run at least once

From there I use a method I wrote to average the values of the array of each color. Then I output the first and second digit I computed of each individual color. Because the hex code of each RGB value is RRGGBB. The pound sign is also output prior to all the values to help indicate it is a hex code. I am not entirely sure what I meant by the comment I wrote.

get the hex codes of each color

The next function is pretty straight forward. It takes in the size of the array and the array itself, adds up each element in the array, and then divides that sum by the size of the array to get the arithmetic average. I could have gotten the size of the array, but the code was hastily put together and passing the size of the array seemed like a straight forward solution. Also, the function returns an integer instead of changing values or anything. Pretty proud of myself for pulling that off all those years ago. Go younger me.

Get the arrays average for the math to get hex codesI'm not sure why I felt the need to tell anyone to Quit their BS, but I suppose that's a great message to current me from previous me. Very self aware. Anyway, as the comment says if the value is a single digit it can be directly converted into HEX. Otherwise there is a little math done to keep within the valid range and preserve its value when converted. What's interesting to me is that in the second function the modulo operator is used instead of the division operator. Toward the end of either function you will notice the hexvalue has a "(char)" on the right hand side of the equation side, that is the way of turning the number into a character because Hex values can be single digits 0-9 or letters A-F.

This is the code to return both hex codes generated by red, green, and blue

All-in-all, the most important part is ensuring the values stay within the applicable range of Hex values. Otherwise it was as simple as just getting user input and processing it properly. From what I recall in Application Development with Visual Basic that summarized the hardest part of a lot of projects. I find it really helps to break it by figuring out how to take the input, and then slowly manipulate that input to get your desired result. Like anything speed comes with time. Since this is in Java you can find the source code on Github here or see the video below for a working demonstration.