![](/img/trans.png)
[英]Is it neccessary to change the RGB color to HSL in order to change specific range of color?
[英]Getting RGB color range for specific colors
我需要獲取一些基本顏色的 RGB 顏色范圍。 ex 紅色介於 RGB(a,b,c) 到 RGB(x,y,z) 之間,明智地或使用其色調值。歡迎提供任何建議。
我認為沒有人能以令人信服的方式回答這個問題,因為這一切都基於回答者的意見。 你真正認為紅色是什么?
考慮G = 0和B = 0 ,現在開始逐步從0增加 R。 人們會爭辯說, (100, 0, 0)對你來說是一個有效的紅色嗎? 或者(150, 0, 0)是你的起始范圍? 你真的需要自己測試一下。
以下完全不科學,僅基於我的觀點:
我會考慮 Red 從(160, 0, 0)開始到(255, 0 0) 。
此外,當R = 255 時。 我會考慮任何值(R = 255,G = g,B = b) ,其中g > b為紅色。 直到達到(g = 200 和 b = 200)時,它才真正開始變成白色。
但是當b > g 時,你會得到一種顏色,在特定范圍內可能被認為是紅色的變體。
結論:對此沒有正確的答案。 您確實需要對其進行測試並檢查什么最符合您的要求。
這是一種在兩個 rgb 值之間提出 rgb 值數組的 javascript 方法。
const lightestColor = 'rgb(204, 224, 245)';
const darkestColor = 'rgb(0, 102, 206)';
const length = 5
const colorArray = this._interpolateColors(
lightestColor,
darkestColor,
length
);
// interpolate between two colors completely, returning an array of rgb values
private _interpolateColors(color1, color2, steps): string[] {
const stepFactor = 1 / (steps - 1);
const rgbArray = [];
color1 = color1.match(/\d+/g).map(Number);
color2 = color2.match(/\d+/g).map(Number);
for (let i = 0; i < steps; i++) {
const color = this._interpolateColor(color1, color2, stepFactor * i);
const string = color.join(', ');
rgbArray.push('rgb(' + string + ')');
}
return rgbArray;
}
// Returns a single rgb color interpolation between given rgb color
// based on the factor given; via https://codepen.io/njmcode/pen/axoyD?editors=0010
private _interpolateColor(color1, color2, factor): number[] {
if (arguments.length < 3) {
factor = 0.5;
}
const result = color1.slice();
for (let i = 0; i < 3; i++) {
result[i] = Math.round(result[i] + factor * (color2[i] - color1[i]));
}
return result;
}
console.log('colorArray. -->', colorArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.