简体   繁体   English

SASS / SCSS-十六进制颜色数学计算

[英]SASS/SCSS - Hex Colors Mathematical Calculations

This question is for learning purposes only. 这个问题仅用于学习目的。 I understand the logic behind math operations in SASS when calculating hex colors, ie: 我了解计算十六进制颜色时SASS中数学运算背后的逻辑,即:

<div>Style me</div>

div {
color: #010203 + #010203;
}

I know it will be calculated in this way: 我知道它将以这种方式计算:

01 + 01 = 02
02 + 02 = 04
03 + 03 = 06

which will compile to: color #020406; 它将编译为: color #020406; .

More precisely: 更确切地说:

div {
color: #020406;
}

How are hex colors calculated when you have letters in them? 当您有字母时,十六进制颜色如何计算? For example: 例如:

div {
color: #a1b1c1 + a2b2c2;
}

Are they calculated like this: 他们是这样计算的吗?

a1 + a2 = a3
b1 + b2 = b3
c1 + c2 = c3

which would compile to color: #a3b3c3; 可以编译成color: #a3b3c3; ?

If so, how would then 如果是这样,那怎么办

div {
color: #a1a2a3 + #b1b2b3;
}

calculate? 计算?

Would a1 + b1 result c2 ? a1 + b1a1 + b1 c2吗?

If it would, what would happen with: 如果可以,将会发生什么:

  • multiplication * 乘法*
  • division / 部门/
  • modulo % %

I know this is a very long question and I apologize for that. 我知道这是一个很长的问题,对此我深表歉意。

Thank you. 谢谢。

In the example of #010203 + #010203 you should first split the string to retrieve RGB channels, so than you can do RED+RED #010203 + #010203的示例中,您应该首先将字符串拆分以检索RGB通道,这样您才能执行RED + RED

 var R1 = parseInt("01", 16); // 1 var R2 = parseInt("01", 16); // 1 var R1R2 = Math.min(R1 + R2, 255); // 2 console.log( R1R2.toString(16) ); // "2" 

add a leading zero and here you go with 02 , repeat for all other channels and you got your #020406 添加一个前导零,然后在这里加上02 ,对所有其他频道重复上述步骤,就得到了#020406

Let's see another example with letters: 让我们来看另一个带有字母的示例:

 var R1 = parseInt("9f", 16); // 195 var R2 = parseInt("ae", 16); // 174 var R1R2 = Math.min(R1 + R2, 255); // 255 <<< !!!!! console.log( R1R2.toString(16) ); // "ff" 

so since R1 + R2 are exceeding 255 max value for RED channel, 255 will be used, 因此,由于R1 + R2超过RED通道的最大值255,因此将使用255,
convert it back to HEX and you have ff . 转换回十六进制,你有ff

To conclude: 结论:

#ff0000 + #00ffff = #ffffff which makes quite sense but let's see further: #ff0000 + #00ffff = #ffffff这很有意义,但让我们进一步了解:
#eeeeee + #eeeeee = #ffffff which could make you conclude every HEX+HEX (R,G or B) channel that sums over 255 stays at 255. (pseudo-code:) Min( R1_16 + R2_16, 255) #eeeeee + #eeeeee = #ffffff ,可能使您得出结论:每个#eeeeee + #eeeeee = #ffffff超过255的HEX + HEX(R,G或B)通道均停留在255。(伪代码:) Min( R1_16 + R2_16, 255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM