如何将 RGB 值从 colormode(255) 更改为 colormode(1)?

How to change RGB values from colormode(255) to colormode(1)?

import turtle

turtle.colormode(255)

turtle.color(130, 50, 50)

如果我将颜色模式设置为 1:turtle.colormode(1),我应该在此处输入什么 RGB 值? turtle.color(?, ?, ?)

谢谢。

设置为1.0意味着rgb值应该在[0, 1.0]范围内,你可以通过将255模式中的每个组件除以[=得到14=]:

import turtle

turtle.colormode(1.0)

turtle.color(130/255, 50/255, 50/255)

文档说:

Return the colormode or set it to 1.0 or 255. Subsequently r, g, b values of color triples have to be in the range 0..cmode.

因此,如果 cmode1,您将需要十进制 RGB 值(下面的 130 / 255、50 / 255、50 / 255):

turtle.color(0.51, 0.197, 0.197)

这对于 colorsys 模块特别有用,其参数也在该范围内:

r, g, b = colorsys.hsv_to_rgb(0.2, 1, 1)  # bright orange HSV
turtle.color(r, g, b)

您的颜色模式设置为 1.0,因此单独的颜色坐标需要 floats0.01.0 范围内.使用 float / 除法运算符。

因此,将 rgb 值除以 255

r = 130/255
g = 50/255
b = 50/255
color = (r, g, b)
turtle.color(color)