the code
/**
* 将十六进制颜色转换为RGB格式
* @param {string} hex 十六进制颜色值
* @return {string} {r, g, b} 返回最终的rgb值
*/
function hexToRgb(hex) {
// 去掉可能的#字符
hex = hex.replace(/^#/, '');
// 如果是缩写形式的颜色值(如 #FFF),则将其扩展为完整形式
if (hex.length === 3) hex = hex.replace(/(.)/g, '$1$1');
// 解析RGB值
let bigint = parseInt(hex, 16),
r = (bigint >> 16) & 255,
g = (bigint >> 8) & 255,
b = bigint & 255;
return {
r, g, b };
}
/**
* 两种颜色相减后的值
* @param {string} color1 颜色1(主颜色,一般是大值)
* @param {string} color2 颜色2
* @return {object} { hexadecimal, rgb } 返回十六进制和rgb值
*/
function subtractColors(color1, color2) {
let rgb1 = hexToRgb(color1),
rgb2 = hexToRgb(color2),
r = rgb1.r - rgb2.r,
g = rgb1.g - rgb2.g,
b = rgb1.b - rgb2.b,
reslut = (val) => Math.max(0, val);
// 将RGB结果转换为十六进制格式
return {
hexadecimal: `#${
(1 << 24 | reslut(r) << 16 | reslut(g) << 8 | reslut(b)).toString(16).slice(1)}`,
rgb: `${
r}, ${
g}, ${
b}`
};
}
console.log(subtractColors('ffffff', 'ff0000'));
// {hexadecimal: '#00ffff', rgb: '0, 255, 255'}
replace
w3school
replace
method searches a string for a value or a regular expression.
replace
method returns a new string with the value replaced.
replace
method does not change the original string.
If you replace a value, only the first instance will be replaced. To replace all instances, useg
a regular expression with modifier sets.
MDN
replace
The method returns a new string withreplacement
some or allpattern
matches of the pattern ( ) replaced by the replacement value ( ). The pattern can be a string or a regular expression, and the replacement value can be a string or a callback function to be called for each match. Ifpattern
a string, only the first occurrence is replaced.
parseInt
MDN
parseInt(string, radix)
Parses a string and returns a decimal integer in the specified radix,radix
which is2-36
an integer between, representing the radix of the string being parsed.
string
The value to be parsed. If the argument is not a string, it is converted to a string (using the ToString abstract operation). Whitespace at the beginning of the string will be ignored.
radix
Optional Integer
from2
to36
, representing the base of the base. For example, specifying16
that the parsed value is a hexadecimal number. Will return if outside of this rangeNaN
. If specified0
or not specified, the radix will be inferred from the value of the string. =The extrapolated result will not always be the default10
! The description at the end of the article explainsradix
the specific behavior of the function when the parameter is not passed.
w3school
parseInt()
The function parses the string and returns an integer.
radix
The parameter is used to specify which number system to use, e.g. radix is16
(hexadecimal) to indicate that the numbers in the string should be parsed from hexadecimal to decimal.
Ifradix
the argument is omitted,JavaScript
the following is assumed:
if the string begins"0x"
with , the radix is16
(hexadecimal)
If the string"0"
starts with , the radix is8
(octal). This attribute is deprecated.
If the string starts with any other value, the base is10
(decimal)
Only returns the first number in the string!
Leading and trailing spaces are allowed.
parseInt()
Returns if the first character cannot be converted to a numberNaN
.
== Older browsers will resultparseInt("010")
in , because8
older versions use an octal radix as the default when the string starts with . From start, the default is base-decimal .ECMAScript
ECMAScript5
"0"
8
ECMAScript5
10
max
MDN
Math.max()
The function returns the largest number taken as an input parameter, or -Infinity if there is no parameter .
W3school
max()
method returns the number with the highest value.
Tip: The min() method returns the number with the smallest value.
w3school
toString()
method returns the object as a string. Return
if the method cannot return a string . Always return the object constructor. method does not change the original object.toString()
"[object Object]"
Object.toString()
toString()
EveryJavaScript
object hastoString()
methods. methods are used internally
when an object needs to be displayed as text (as inHTML
) or when an object needs to be used as a string . Normally, you wouldn't use it in your own code.JavaScript
toString()
MDN
toString()
method returns a string representing the object. This method is intended to override (customize) the logic of type conversions for derived class objects .
slice
w3school
slice
The method returns the selected elements in the array with a new array object.
slice
method selectsstart
elements starting at and ending at, but not including, the givenend
argument.
slice
method does not alter the original array.
MDN
slice
The method returns a new array object, which is a shallow copy of the original array determined by and, inclusivestart
, excluding , where and represent the index of the array element. The original array will not be changed.end
start
end
start
end