0

I am using the turtle module in python and I want to obtain a numeric representation (RGB, Hex, etc.) of the color instead of the name as a string.

from turtle import Screen
print(Screen().bgcolor())

The example above prints >>> white and I need to get a value like >>> #FFFFFF or >>> rbg(255, 255, 255) instead.

I am looking for creating complementary colors for arbitrary values so the color name is not useful enough.

1 Answer 1

1

This is a situation where we need to poke under the hood and access tkinter upon which turtle sits:

from turtle import Screen

screen = Screen()
canvas = screen.getcanvas()
root = canvas.winfo_toplevel()

r, g, b = root.winfo_rgb(screen.bgcolor())

print(r, g, b)

OUTPUT

% python3 test.py
65535 65535 65535
% 

To pass these values back into turtle, we'll either need to (floating point) divide them by 65535 if we're using the default turtle color model, or shift them by 8 bits for the alternative colormode(255).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.