Tom Liu
Tom Liu

Reputation: 45

How to set RGB value in Python?

import turtle as t
t.Screen().bgcolor('#8A360F')

For above scripts, is there a way that I can set RGB(138, 54, 15) instead of '#8A360F'?

e.g.

t.Screen().bgcolor(RGB(138, 54, 15))

Thanks.

Upvotes: 0

Views: 1134

Answers (2)

NodeX4
NodeX4

Reputation: 49

You can Use the :x operator and make a custom formula

def rgb_to_hex(r, g, b):
      return ('{:X}{:X}{:X}'). format(r, g, b)

Now call this within the bgcolor function, It works because :x makes normal values become hex values

import turtle as t
t.Screen().bgcolor(rgb_to_hex(100, 120, 90))

Upvotes: 2

You can use it in this way

import turtle as t
t.pencolor(83, 58, 27)

Upvotes: 0

Related Questions