如何使用 SASS 等颜色但在 Ruby 中进行数学运算?

发布于 2024-08-30 16:19:46 字数 119 浏览 3 评论 0原文

在 SASS 中我可以做:

!pink = #ff43a7
!darker_pink = !pink - #333333

我想在 Ruby 中做同样的事情。

In SASS I can do:

!pink = #ff43a7
!darker_pink = !pink - #333333

I'd like to the same in Ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南渊 2024-09-06 16:19:46

可以通过在值前添加 0x 来在 Ruby 中表示十六进制:

pink = 0xff43a7
darker_pink = pink - 0x333333

color helper

def color(hex)
  "#%06x" % hex
end

在 ERb 模板

.container {
  color: <%= color pink %>;
  border: 1px solid <%= color darker_pink %>;
}

输出中的用法

.container {
  color: #ff43a7;
  border: 1px solid #cc1074;
}

Hex can be represented in Ruby by prefixing your value with 0x:

pink = 0xff43a7
darker_pink = pink - 0x333333

color helper

def color(hex)
  "#%06x" % hex
end

Usage in ERb template

.container {
  color: <%= color pink %>;
  border: 1px solid <%= color darker_pink %>;
}

Output

.container {
  color: #ff43a7;
  border: 1px solid #cc1074;
}
捶死心动 2024-09-06 16:19:46

使用 Sass 模块

如果您已经拥有 Sass 库,则可以实例化并使用其对象。

例如:

red  = Sass::Script::Color.new([255, 0, 0])
gray = Sass::Script::Color.new([128, 128, 128])
red.lightness < gray.lightness # => true

必须有一种内置方法可以将像 #00FF00 这样的十六进制字符串转换为 Color 对象,但没有看到,我写了这个函数:

# @param color_string - hex string, like '#22FF22'. MUST be 6 characters,
# because I don't feel like dealing with the use-case for 3. :)
def color_from_hex_string(color_string)
  # Drop the leading '#', if any
  color_string = color_string[1..-1] if color_string.start_with?('#')
  raise ArgumentError.new('Hex string must be 6 characters') unless color_string.length == 6

  # Turn into array of 2-digit decimal numbers. 
  # Eg, '808080' becomes [128, 128, 128]; '#ff0000' becomes [255, 0, 0]
  rgb_array = color_string.split('').each_slice(2).map do |slice|
    slice.join('').to_i(16).to_s(10)
  end

  # Use that to build a new Color object
  color = Sass::Script::Color.new(rgb_array)

  # Set this option so it won't complain (?)
  color.options = {:style => :compressed}

  return color
end

Use the Sass module

If you already have the Sass library, you can instantiate and work with its objects.

For instance:

red  = Sass::Script::Color.new([255, 0, 0])
gray = Sass::Script::Color.new([128, 128, 128])
red.lightness < gray.lightness # => true

There must be a built-in way to turn hexadecimal strings like #00FF00 into Color objects, but not seeing one, I wrote this function:

# @param color_string - hex string, like '#22FF22'. MUST be 6 characters,
# because I don't feel like dealing with the use-case for 3. :)
def color_from_hex_string(color_string)
  # Drop the leading '#', if any
  color_string = color_string[1..-1] if color_string.start_with?('#')
  raise ArgumentError.new('Hex string must be 6 characters') unless color_string.length == 6

  # Turn into array of 2-digit decimal numbers. 
  # Eg, '808080' becomes [128, 128, 128]; '#ff0000' becomes [255, 0, 0]
  rgb_array = color_string.split('').each_slice(2).map do |slice|
    slice.join('').to_i(16).to_s(10)
  end

  # Use that to build a new Color object
  color = Sass::Script::Color.new(rgb_array)

  # Set this option so it won't complain (?)
  color.options = {:style => :compressed}

  return color
end
雨巷深深 2024-09-06 16:19:46

在 Sass 中添加/减去颜色的基本方法是无意义的,只有在使用灰度调整时才真正有效。这就是为什么在 Sass 3 中,我们现在完全支持 HSL 域中的操作,这与人们对颜色的看法密切相关。

由于 Sass 是用 Ruby 编写的,因此您至少可以阅读我们的代码来了解发生了什么。

这是 颜色类,以及 < a href="http://github.com/nex3/haml/blob/master/lib/sass/script/functions.rb" rel="nofollow noreferrer">对其进行操作的函数。

这确实是不平凡的代码。为什么不直接使用 Sass 呢?

The basic approach of adding/subtracting colors in Sass is nonsense and only really works when using a gray adjustment. That's why in Sass 3 we now have full support of operations in the HSL domain which maps closely to the way people think about colors.

Since Sass is written in Ruby, you can at least read our code to see what's going on.

Here's the Color class, and the functions that operate on them.

It really is non-trivial code. Why not just use Sass?

朦胧时间 2024-09-06 16:19:46

为了完善@macek的答案,遵循@drawnownward和@lpsquiggle的愿望:

你可以制作两个助手,如下所示:

  def color(color)
    "#%06x" % color
  end

  def darker_color(color)
    x = color.match(/0x(..)(..)(..)/)
    r = x[1].sub(/[0-3]/, '5')
    g = x[2].sub(/[0-3]/, '5')
    b = x[3].sub(/[0-3]/, '5')
    rgb = "0x#{r}#{g}#{b}"
    "#%06x" % (rgb.hex - 0x444444)
  end

优点:如果你定义了一个低值的颜色十六进制(这里是0到3之间),这些将被碰撞在减法之前,它们最终会变成 0,而不是环绕并变成 c、d、e 或 f(这会给你一个意想不到的颜色)。它只对每个 #rrggbb 对中的第一个值执行此操作,因此 #313131 变为 #0d0d0d,这在技术上是不正确的,但它比 #fdfdfd 好得多,所以这似乎是一个足够好的妥协,因为你会想要在其他情况下保留第二个值。

那么,在您的 Erb 模板中,您可以这样写:

.container {
  color: <%= color pink %>;
  border: 1px solid <%= darker_color pink %>;
}

而不是:

.container {
  color: <%= color pink %>;
  border: 1px solid <%= color darker_pink %>;
}

希望对某人有帮助。

To refine @macek's answer, following @drawnownward's and @lpsquiggle's wishes:

You can make two helpers, like so:

  def color(color)
    "#%06x" % color
  end

  def darker_color(color)
    x = color.match(/0x(..)(..)(..)/)
    r = x[1].sub(/[0-3]/, '5')
    g = x[2].sub(/[0-3]/, '5')
    b = x[3].sub(/[0-3]/, '5')
    rgb = "0x#{r}#{g}#{b}"
    "#%06x" % (rgb.hex - 0x444444)
  end

The advantage: if you've defined a color hex with low values (between 0 and 3, here), these will be bumped up before the subtraction, so that they end up as 0 afterward, instead of wrapping around and becoming c, d, e, or f (which would give you a color you didn't expect). It only does this for the first value in each #rrggbb pair, so #313131 becomes #0d0d0d, which isn't technically correct, but it's much better than #fdfdfd, so it seems like a good enough compromise, since you'll want to keep those second values in other cases.

In your Erb template, then, you would write this:

.container {
  color: <%= color pink %>;
  border: 1px solid <%= darker_color pink %>;
}

Instead of:

.container {
  color: <%= color pink %>;
  border: 1px solid <%= color darker_pink %>;
}

Hope that helps someone.

攒眉千度 2024-09-06 16:19:46

我刚刚遇到一些问题,我想将一组颜色分配给不同的色调。 SASS 源没有多大帮助,因为我没有找到从 HSV 获取 RGB 的方法。

彩色宝石有我需要的东西。

我写了这个助手:

def region_color(index, size)
  h = (index.to_f / (size - 1).to_f)
  Color::HSL.from_fraction(h, 0.95, 0.3).html
end

I just came across something where I wanted to distribute colors for a set across different hues. The SASS source didn't help much because I didn't see a way to get RGB from HSV.

The color gem had what I needed.

I wrote this helper:

def region_color(index, size)
  h = (index.to_f / (size - 1).to_f)
  Color::HSL.from_fraction(h, 0.95, 0.3).html
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文