2011-10-08 175 views
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”?我可以只追加一个字符串吗?

+0

你能写一些伪代码来显示你想通过频道迭代吗? –

+0

查看我上面的编辑:) – Tixz

回答

0

这是你的代码略有变化:

module Sass::Script::Functions 
    def union(b, s) 
    b + s - (b * s) 
    end 

    def multiply(top, bottom) 
    assert_type top, :Color 
    assert_type bottom, :Color 

    blend do |first_color, second_color| 
     multiply_channel(first_color, second_color) 
    end 
    end 

    def multiply_channel(cb, cs) 
    cb * cs/255 
    end 

    def darken(top, bottom) 
    assert_type top, :Color 
    assert_type bottom, :Color 

    blend(bottom, top) do |first_color, second_color| 
     darken_channel(first_color, second_color) 
    end 
    end 

    def blend(channel_one, channel_two, &block) 
    red = color_composite(channel_one.red, channel_one.alpha, channel_two.red, channel_two.alpha, block.call(channel_one.red, channel_two.red)); 
    green = color_composite(channel_one.green, channel_one.alpha, channel_two.green, channel_two.alpha, block.call(channel_one.green, channel_two.green)); 
    blue = color_composite(channel_one.blue, channel_one.alpha, channel_two.blue, channel_two.alpha, block.call(channel_one.blue, channel_two.blue)); 

    return [red, green, blue, union(channel_one.alpha, channel_two.alpha)]; 
    end 

    def darken_channel(cb, cs) 
    [cb, cs].min 
    end 

    def color_composite(cb, ab, cs, as, fnc) 
    ((1 - as) * ab * cb) + ((1- ab) * as * cs) + (ab * as * fnc) 
    end 

end 

现在,混合方法有两个参数:

channel_one, channel_two 

,并接受块。 混合通过两块这里两个参数和其他两个地方:

block.call(channel_one.red, channel_two.red) 

调用执行块,它的参数传递到阻止:

blend do |first_color, second_color| 
    multiply_channel(first_color, second_color) 
end 

所以first_color成为channel_one.redsecond_color成为channel_two.red。 在每个block.call上调用该块。

希望得到这个帮助。