0
我是新来的红宝石,但与其他语言如PHP,Python和JavaScript的等伟大的经验红宝石模块优化
我一直在写这个模块实现的红宝石混合模式,习惯语言。不过,我认为可以做出很多优化和自动化。我一直在浏览文档,但很难找到我要找的内容,因为我不知道我想使用的技术的激情技术术语。
这里是我的模块的一小部分:
module Sass::Script::Functions
def union(b, s)
return b + s - (b * s)
end
def color_composite(cb, ab, cs, as, fnc)
return ((1 - as) * ab * cb) + ((1- ab) * as * cs) + (ab * as * fnc)
end
def multiply_channel(cb, cs)
return cb * cs/255
end
#MULTIPLY
def multiply(top, bottom)
assert_type top, :Color
assert_type bottom, :Color
red = color_composite(bottom.red, bottom.alpha, top.red, top.alpha, multiply_channel(bottom.red, top.red));
green = color_composite(bottom.green, bottom.alpha, top.green, top.alpha, multiply_channel(bottom.green, top.green));
blue = color_composite(bottom.blue, bottom.alpha, top.blue, top.alpha, multiply_channel(bottom.blue, top.blue));
return [red, green, blue, union(bottom.alpha, top.alpha)];
end
def darken_channel(cb, cs)
return [cb, cs].min
end
#DARKEN
def darken(top, bottom)
assert_type top, :Color
assert_type bottom, :Color
red = color_composite(bottom.red, bottom.alpha, top.red, top.alpha, darken_channel(bottom.red, top.red));
green = color_composite(bottom.green, bottom.alpha, top.green, top.alpha, darken_channel(bottom.green, top.green));
blue = color_composite(bottom.blue, bottom.alpha, top.blue, top.alpha, darken_channel(bottom.blue, top.blue));
return [red, green, blue, union(bottom.alpha, top.alpha)];
end
end
这是我想要做什么:
- 有一个功能来处理不同的渠道,并采取混合模式作为参数。例如。混合(顶部,底部,“乘”)
- 能够迭代虽然三个渠道以减少重复
如果你可以点我将不胜appriciated正确的方向!
编辑
我想遍历这样的颜色:
color = []
for (red,green,blue) as channel
color << color_composit(bottom.channel, bottom.alpha, top.channel, top.alpha, blendmode(bottom.channel, top.channel))
也可以与你进行自动化说的代码更是这样,我不需要定义在每个通道上运行darken_channel的函数变暗,但只调用混合函数并将“变暗”作为参数传递,然后它自己附加“_channel”?我可以只追加一个字符串吗?
你能写一些伪代码来显示你想通过频道迭代吗? –
查看我上面的编辑:) – Tixz