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

Less中darken函数嵌套与直接调用的结果差异疑问

Why darken(darken(#ffffff, 3), 5) != darken(#f7f7f7, 5) in Less?

Great question! These two calls are not equivalent, and the difference boils down to how Less handles color values internally vs. serialized RGB strings, plus precision math behind HSL-to-RGB conversions. Let's break it down step by step:

1. How Less's darken() function works

darken() operates on the HSL color space: it converts the input color to HSL, decreases the lightness component by the specified percentage, then converts the result back to RGB (since CSS uses RGB/hex for colors).

The key detail here is: when you chain darken() calls directly (nested), Less keeps the intermediate color as an internal HSL object (no rounding to integer RGB values yet). But when you use a serialized hex value like #f7f7f7, it has to convert that hex back to HSL first—introducing a tiny precision loss.

2. Let's calculate both scenarios

Scenario 1: Nested darken(darken(#ffffff, 3), 5)

  • Step 1: darken(#ffffff, 3) takes pure white (HSL: 0°, 0%, 100%), reduces lightness by 3% to 97%. Instead of immediately converting to RGB (which would round 255 * 0.97 = 247.35 to 247 / #f7), Less keeps this as an HSL value (0°, 0%, 97%).
  • Step 2: The second darken(..., 5) takes that HSL value, reduces lightness by another 5% to 92%. Converting 92% lightness to RGB gives 255 * 0.92 = 234.6, which rounds up to 235 (hex eb). Result: #ebebeb.

Scenario 2: Direct darken(#f7f7f7, 5)

  • #f7f7f7 is a serialized hex color, which translates to RGB values 247, 247, 247. When Less converts this to HSL, the lightness is calculated as 247 / 255 ≈ 96.86% (not the exact 97% from the nested step).
  • Reducing that lightness by 5% gives 96.86% - 5% = 91.86%. Converting back to RGB: 255 * 0.9186 ≈ 234.25, which rounds down to 234 (hex ea). Result: #eaeaea.

3. The core takeaway

The nested call avoids the intermediate RGB rounding step, preserving the exact HSL lightness value for the second adjustment. Using the serialized hex value introduces a small precision error because the rounded RGB can't perfectly represent the original HSL lightness.

If you want to make the direct call match the nested result, you can keep the intermediate color as a Less variable (which preserves the internal color data instead of serializing it):

@light-gray: darken(#ffffff, 3);
@darker-gray: darken(@light-gray, 5); // This will equal #ebebeb

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:34:02