You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助编写基于用户输入百分比计算RGB值的Python程序

How to Calculate RGB Values Based on Percentage Input (For Total Beginners)

Hey there! Since you're totally new to Python, let's break this down step by step so you can understand exactly what's going on. Your goal is to take a 0-100% input and return the corresponding RGB value that transitions smoothly between three preset colors—let's make this happen.

First, Let's Understand the Problem

You have three key color points to work with:

  • 0% → [0, 255, 0] (bright green)
  • 50% → [255, 128, 0] (orange-yellow)
  • 100% → [255, 0, 0] (bright red)

We need to do linear interpolation—that's just a simple way to calculate the color that sits exactly between two points based on your percentage. For example, 33% is 66% of the way from 0% to 50%, so we'll compute each RGB channel separately to get the right mix.

Step-by-Step Code Implementation

Let's modify your initial code and add the logic we need. I'll add comments to explain every line so you can follow along:

# Define our three base colors at 0%, 50%, and 100%
colour0percent = [0, 255, 0]
colour50percent = [255, 128, 0]
colour100percent = [255, 0, 0]

# Get user input and convert it to an integer (since we're dealing with whole percentages)
percentage = int(input("Input: "))

# Initialize an empty list to store our final RGB color
colour = []

# Calculate the RGB values based on which range the percentage falls into
if percentage <= 0:
    # If input is 0 or lower, just use the 0% color
    colour = colour0percent
elif percentage <= 50:
    # For 1-50%: interpolate between 0% and 50% colors
    # Calculate how far along we are from 0% to 50% (a value between 0.0 and 1.0)
    factor = percentage / 50
    # Calculate each RGB channel separately
    red = int(colour0percent[0] + factor * (colour50percent[0] - colour0percent[0]))
    green = int(colour0percent[1] + factor * (colour50percent[1] - colour0percent[1]))
    blue = int(colour0percent[2] + factor * (colour50percent[2] - colour0percent[2]))
    colour = [red, green, blue]
elif percentage <= 100:
    # For 51-100%: interpolate between 50% and 100% colors
    # Calculate how far along we are from 50% to 100% (a value between 0.0 and 1.0)
    factor = (percentage - 50) / 50
    red = int(colour50percent[0] + factor * (colour100percent[0] - colour50percent[0]))
    green = int(colour50percent[1] + factor * (colour100percent[1] - colour50percent[1]))
    blue = int(colour50percent[2] + factor * (colour100percent[2] - colour50percent[2]))
    colour = [red, green, blue]
else:
    # If input is over 100, just use the 100% color
    colour = colour100percent

# Print the result in the format you requested
print(f"{percentage}, {colour}")

Let's Test It!

  • If you input 50, you'll get: 50, [255, 128, 0] (exactly what you expected)
  • If you input 33, let's confirm the math manually:
    • Factor = 33/50 = 0.66
    • Red: 0 + 0.66*(255-0) = 168.3 → rounded to 168
    • Green: 255 + 0.66*(128-255) = 255 - 83.82 = 171.18 → rounded to 171
    • Blue: 0 + 0.66*(0-0) = 0
    • So output is 33, [168, 171, 0]—perfect, that's exactly what you wanted!

Key Things to Note

  • We use int() to round down the calculated values because RGB values have to be whole numbers between 0 and 255.
  • We added checks for values outside 0-100 to make the program more robust (it'll just use the closest base color if someone inputs a number outside the range).
  • Each RGB channel is calculated independently because each one transitions at a different rate (green goes from 255 → 128 → 0, while red stays at 255 after the 50% mark).

内容的提问来源于stack exchange,提问作者Lukas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:05:58