ruby​​ shoes从ask_color转换为十六进制代码

问题描述:

我已经尝试了很多次来解决这个问题。请帮帮我。ruby​​ shoes从ask_color转换为十六进制代码

我创造了这个代码:

Shoes.app do 
    button "Color" do 
    @giv_color=ask_color("Seleziona un colore") 
    def rgb(r, g, b) 
     "##{to_hex r}#{to_hex g}#{to_hex b}" 
    end 
    def to_hex(n) 
     n.to_s(16).rjust(2, '0').upcase 
    end 
    para @giv_color # => this give me a result in rgb of a selected color (es. rgb(20, 20, 40)) 
    para rgb(100, 200, 300) #=> this give me a correct hex color convetided 
    end 
end 

我不理解为什么我不是以十六进制代码自动转换值RGB。

+0

这段代码甚至不会运行,因为它缺少'end'。另外'def rgb(r,g,b); end'定义了一个空方法。你确定你知道问题是什么吗? – Max

+0

这个问题不是代码,因为完美的工作,问题是我不能转换十六进制代码@giv_color。在评论之前尝试我的代码。 – awar

+0

@awar为'@ giv_color'提供了一个值的示例 - 它返回的内容以及您希望它返回的内容。我猜测用户从颜色选择器中选择了一个? – OneNeptune

经过多次尝试,我找到了自己的解决方案。我没有意识到我从颜色选择返回的是一个字符串,清理后者并转换为整数,我已经解决了谜题。谢谢你的帮助。

#!/usr/bin/ruby 
Shoes.app do 
    button "Color" do 
    @giv_color=ask_color("Seleziona un colore") 
    def rgb(r, g, b) 
     "#{to_hex r}#{to_hex g}#{to_hex b}" 
     end 
    def to_hex(n) 
     n.to_s(16).rjust(2, '0').upcase 
end 
    arr = @giv_color.inspect.tr('rgb()','').split(',') # clean string returned from selected color 
    a = arr[0].to_i #--| 
    b = arr[1].to_i # | ---- convert the string number on integer 
    c = arr[2].to_i #--| 

    hex = rgb(a, b, c) 
    para hex # <<--- return the hex code 
    end 

end