rubycolorshexrgbshoes

ruby shoes convert to hex code from ask_color


I have tried many times to resolve this problem. Please help me.

I created this code:

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

I'm not understanding why I am not converting the value rgb in hex code automatically.


Solution

  • I found the solution on its own after several attempts. I did not realize what I was returning from the color selection was a string, clean the latter and converted to integer numbers I have solved the riddle. Thanks for your help.

    #!/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