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

请求实现:基于自定义RGB刻度,根据0-100数值返回RGB值

Mapping 0-100 Double Values to Custom RGB Colors

Let's tackle this problem directly—you need to convert a double value between 0 and 100 to an RGB color based on those specific range rules. The key here is using linear interpolation between the start and end RGB values for each interval, since the example (like 0.5 returning 145,221,249) clearly shows a smooth transition between the two endpoints of the 0-1 range.

Step 1: Define the Range Rules Clearly

First, let's restate your custom ranges with their start/end RGB pairs to avoid confusion:

  • Exact value 0.0: RGB (255, 255, 255)
  • 0 < value < 1: Transition from (165, 242, 243) to (125, 200, 255)
  • 1 ≤ value < 25: Transition from (0, 255, 0) to (63, 82, 0)
  • 25 ≤ value < 50: Transition from (63, 82, 0) to (255, 143, 31)
  • 50 ≤ value < 75: Transition from (255, 143, 31) to (255, 0, 0)
  • 75 ≤ value ≤ 100: RGB (255, 0, 0) (no transition since start/end are identical)

Step 2: Linear Interpolation Formula

For each RGB channel (R, G, B) in a transition range, calculate the value using this formula:

channel_value = start_channel + (end_channel - start_channel) * (current_value - range_start) / (range_end - range_start)

Then round to the nearest integer (since RGB values are 0-255 integers).

Step 3: Example Implementation (Python)

Here's a clean function that implements this logic, with test cases matching your examples:

def get_custom_rgb(value):
    # Ensure the input stays within 0-100 to handle edge cases
    clamped_value = max(0.0, min(100.0, value))
    
    # Handle exact 0.0 case first
    if clamped_value == 0.0:
        return (255, 255, 255)
    # 0 < value < 1
    elif 0 < clamped_value < 1:
        ratio = clamped_value / 1.0
        r = 165 + (125 - 165) * ratio
        g = 242 + (200 - 242) * ratio
        b = 243 + (255 - 243) * ratio
    # 1 ≤ value < 25
    elif 1 <= clamped_value < 25:
        ratio = (clamped_value - 1) / (25 - 1)
        r = 0 + (63 - 0) * ratio
        g = 255 + (82 - 255) * ratio
        b = 0  # No change in blue channel here
    # 25 ≤ value < 50
    elif 25 <= clamped_value < 50:
        ratio = (clamped_value - 25) / (50 - 25)
        r = 63 + (255 - 63) * ratio
        g = 82 + (143 - 82) * ratio
        b = 0 + (31 - 0) * ratio
    # 50 ≤ value < 75
    elif 50 <= clamped_value < 75:
        ratio = (clamped_value - 50) / (75 - 50)
        r = 255  # No change in red channel here
        g = 143 + (0 - 143) * ratio
        b = 31 + (0 - 31) * ratio
    # 75 ≤ value ≤ 100
    else:
        return (255, 0, 0)
    
    # Convert to integers (rounding ensures we match the example for 0.5)
    return (round(r), round(g), round(b))

# Test the provided examples
print(get_custom_rgb(0.0))   # Output: (255, 255, 255)
print(get_custom_rgb(0.5))   # Output: (145, 221, 249) ✔️
print(get_custom_rgb(1.0))   # Output: (0, 255, 0) ✔️

Key Notes

  • Clamping: The function first clamps the input to 0-100 to handle accidental out-of-range values.
  • Exact Boundaries: For values like 1.0, 25.0, etc., we use the start of the next interval (which matches your example for 1.0 returning (0,255,0)).
  • No-Transition Ranges: The 75-100 range uses the same start/end RGB, so we just return that value directly.

This logic translates easily to other languages (Java, C#, JavaScript, etc.)—the core interpolation math stays the same, only the syntax changes.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:07:09