如何实现两十六进制颜色间的均匀增量生成?含Python输入代码
Generate Uniform Color Gradient Between Two Hex Colors
Got it, let's build out the logic to generate those evenly spaced colors between your two input hex values. Here's a step-by-step breakdown and complete code solution:
Step 1: Understand the Core Math
When you want generate intermediate colors, you end up with a total of generate + 2 colors (including the two input values). To get perfectly even spacing, each RGB channel (red, green, blue) needs to increment by a consistent amount from the start to end color.
For your example input (0x030303, 0x454545, 3 intermediates):
- Total colors to generate: 3 + 2 = 5
- Each channel's total difference: 69 - 3 = 66 (since
0x45is 69 in decimal,0x03is 3) - Step per channel: 66 / (5 - 1) = 16.5 → we'll round to integer values since RGB channels only accept 0-255 integers.
Step 2: Complete Code Implementation
Here's the full code that builds on your existing input logic and adds the gradient generation:
# Get user inputs first_color = int(input("Input first color: "), 16) second_color = int(input("Input second color: "), 16) generate = int(input("Enter the number of colors I will make between the two input numbers: ")) # Helper function to split a hex color integer into RGB channels def split_rgb(color): red = (color >> 16) & 0xFF green = (color >> 8) & 0xFF blue = color & 0xFF return red, green, blue # Split both input colors into their RGB components r_start, g_start, b_start = split_rgb(first_color) r_end, g_end, b_end = split_rgb(second_color) total_colors = generate + 2 gradient = [] for i in range(total_colors): # Calculate how far we are from the start color (0 = start, 1 = end) progress_ratio = i / (total_colors - 1) # Compute each channel's value, rounding to a valid integer current_r = round(r_start + progress_ratio * (r_end - r_start)) current_g = round(g_start + progress_ratio * (g_end - g_start)) current_b = round(b_start + progress_ratio * (b_end - b_start)) # Convert back to a formatted hex string (with leading zeros for single-digit values) hex_color = f"0x{current_r:02X}{current_g:02X}{current_b:02X}" gradient.append(hex_color) # Print the final gradient print("Generated gradient colors:") for color in gradient: print(color)
Step 3: Test with Your Example
If you input:
0x030303as the first color0x454545as the second color3as the number of intermediates
You'll get this output:
Generated gradient colors: 0x030303 0x141414 0x252525 0x363636 0x454545
Key Details to Note
- Channel Splitting: Bitwise operations (
>>and& 0xFF) safely extract each 8-bit RGB component from the integer color value—no messy string parsing needed. - Progress Ratio: This ensures we step evenly from the start to end color, even if the RGB channels have different total ranges.
- Rounding: RGB values must be integers, so rounding ensures we get valid 0-255 values instead of floating-point numbers.
- Hex Formatting:
:02Xguarantees each channel is a two-digit uppercase hex string (e.g.,3becomes03to maintain the proper 6-digit hex color format).
内容的提问来源于stack exchange,提问作者Mezza
相关产品推荐
相关产品推荐

