处理多项检查的最佳方式 ruby
Best way to handle multiple checks ruby
所以我正在尝试编写一个 HSL 到 RGB 的转换器(并最终转换为十六进制)
我正在关注这个 colorspace conversion theory,但我似乎卡在了第 6 步
Now we need to do up to 3 tests to select the correct formula for each
color channel. Let’s start with Red.
test 1 – If 6 x temporary_R is smaller then 1, Red = temporary_2 +
(temporary_1 – temporary_2) x 6 x temporary_R In the case the first
test is larger then 1 check the following
test 2 – If 2 x temporary_R is smaller then 1, Red = temporary_1 In
the case the second test also is larger then 1 do the following
test 3 – If 3 x temporary_R is smaller then 2, Red = temporary_2 +
(temporary_1 – temporary_2) x (0.666 – temporary_R) x 6 In the case
the third test also is larger then 2 you do the following
Red = temporary_2
Ok lets do it for our Red value
6 x temporary_R = 6 x 0.869 = 5.214, so it’s larger then 1, we need to
do test 2 2 x temporary_R = 2 x 0.869 = 1.738, it’s also larger then
1, we need to do test 3 3 x temporary_R = 3 x 0.869 = 2.607, it’s
larger then 2, so we go for the last formula Red = temporary_2 =
0.0924 which rounded down is 0.09, which is a number we recognize from the RGB to HSL conversion
到目前为止,我已经猴子修补了一个函数来获取我的 HSL 颜色
def toRGB(hue, sat, lum)
temp_1 =
case lum
when lum < 0.0
lum x (1.0 * sat)
when lum > 0.0
(lum + sat) - lum
end
temp_2 = (2 * lum) - temp_1.to_f
h = (hue/360.0).round(4)
temp_r = (h + 0.333).round(4)
temp_r = temp_r + 1 if temp_r < 0
temp_r = temp_r - 1 if temp_r > 1
temp_g = h
temp_b = (h - 0.333).round(4)
temp_b = temp_b + 1 if temp_b < 0
temp_b = temp_b - 1 if temp_b > 1
red =
#test 1
#test 2
#test 3
"#{red}"
end
我试图使用 case
语句
red =
case temp_r
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
end
但后来我开始重新阅读说明,现在我真的看不出有什么方法可以在 ruby 中完成我需要的事情。可能是我多虑了。
如果您想在上下文中查看我的其余代码,您 can see it here
我在您的代码片段中注意到两件事:
您在 ruby 中混合了两种类型的 switch case 语句。
case a
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
end
case
when a > 1 && a < 5
"It's between 1 and 5"
when a == 6
"It's 6"
end
看到区别,在第一种情况下,当你直接比较案例时,你必须提到你正在比较的变量名称,而在第二种情况下,你通过显式地放置一个条件来比较 true & false 条件在你的变量上,所以你不需要把它放在大小写旁边。
第二件事是根据你的条件语句,你的 case 语句中需要一个 else case。
red =
case
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
else
temp_2
end
所以我正在尝试编写一个 HSL 到 RGB 的转换器(并最终转换为十六进制) 我正在关注这个 colorspace conversion theory,但我似乎卡在了第 6 步
Now we need to do up to 3 tests to select the correct formula for each color channel. Let’s start with Red.
test 1 – If 6 x temporary_R is smaller then 1, Red = temporary_2 + (temporary_1 – temporary_2) x 6 x temporary_R In the case the first test is larger then 1 check the following
test 2 – If 2 x temporary_R is smaller then 1, Red = temporary_1 In the case the second test also is larger then 1 do the following
test 3 – If 3 x temporary_R is smaller then 2, Red = temporary_2 + (temporary_1 – temporary_2) x (0.666 – temporary_R) x 6 In the case the third test also is larger then 2 you do the following
Red = temporary_2
Ok lets do it for our Red value
6 x temporary_R = 6 x 0.869 = 5.214, so it’s larger then 1, we need to do test 2 2 x temporary_R = 2 x 0.869 = 1.738, it’s also larger then 1, we need to do test 3 3 x temporary_R = 3 x 0.869 = 2.607, it’s larger then 2, so we go for the last formula Red = temporary_2 = 0.0924 which rounded down is 0.09, which is a number we recognize from the RGB to HSL conversion
到目前为止,我已经猴子修补了一个函数来获取我的 HSL 颜色
def toRGB(hue, sat, lum)
temp_1 =
case lum
when lum < 0.0
lum x (1.0 * sat)
when lum > 0.0
(lum + sat) - lum
end
temp_2 = (2 * lum) - temp_1.to_f
h = (hue/360.0).round(4)
temp_r = (h + 0.333).round(4)
temp_r = temp_r + 1 if temp_r < 0
temp_r = temp_r - 1 if temp_r > 1
temp_g = h
temp_b = (h - 0.333).round(4)
temp_b = temp_b + 1 if temp_b < 0
temp_b = temp_b - 1 if temp_b > 1
red =
#test 1
#test 2
#test 3
"#{red}"
end
我试图使用 case
语句
red =
case temp_r
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
end
但后来我开始重新阅读说明,现在我真的看不出有什么方法可以在 ruby 中完成我需要的事情。可能是我多虑了。
如果您想在上下文中查看我的其余代码,您 can see it here
我在您的代码片段中注意到两件事:
您在 ruby 中混合了两种类型的 switch case 语句。
case a
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
end
case
when a > 1 && a < 5
"It's between 1 and 5"
when a == 6
"It's 6"
end
看到区别,在第一种情况下,当你直接比较案例时,你必须提到你正在比较的变量名称,而在第二种情况下,你通过显式地放置一个条件来比较 true & false 条件在你的变量上,所以你不需要把它放在大小写旁边。
第二件事是根据你的条件语句,你的 case 语句中需要一个 else case。
red =
case
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
else
temp_2
end