-2

I am working on a school project where we are supposed to retrieve three color codes (for red, green and blue) from a number between 0-16777215 (because 256x256x256=16777216). I am unable to understand how this number can produce three color codes.

The following code was part A of the task, which I have resolved:

background = input ('Choose background color:')
innersq = input ('Choose color of inner square:')

# set up drawing
setup(330, 330, 0, 0)
screensize(315, 315)
goto(-60, 150)

# choosing colors
bgcolor(background)
color(innersq)

This code was made so that the user would enter a color green, blue, red etc.

The new code is supposed to get the input 0-16777215 and calculate a color code for each of the three main colors.

The last part of the code should end up something like this:

#choosing colors
bgcolor(Rb0-255, Gb0-255, Bb0-255)
color (Rb0-255, Gb0-255, Bb0-255)
Prune
  • 76,765
  • 14
  • 60
  • 81
vault
  • 124
  • 1
  • 9

1 Answers1

1

This is one standard representation for a color. Think of the input as being in binary or hexadecimal, rather than decimal. The first 8 bits are the red part, the middle 8 bits are the green part, and the last 8 bits are the blue part.

You'll likely want to use integer division to move the wanted part to the right side of your number, then use the modulus operator, %, to grab the eight bits needed. Another way to attack the same idea is to use the bit-wise and operator, &, to get those last bits.

That should be enough of a hint to get you moving.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I don't understand. It would be very helpful if you would tell me the math of how to get the desired colorcode-numbers from the input number – vault Aug 31 '18 at 16:54