Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Help with Color Coordinates please — Gideros Forum

Help with Color Coordinates please

MoKaLuxMoKaLux Member
edited September 2019 in General questions
I need your help please:
-I have an RGB image size (306 * 48)
-I have an image (can be any size)

I have a color picker, when I pick a color on the image I would like to get its x position on the RGB image.
I have been trying everything that came to my mind but did not succeed.

Do you know a formula to do it?

Thank you in advance for your help.
function SlideColorPicker:setHue(xhexcolor)
	--self.colors = {0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000}
	self.e = Event.new("COLOR_CHANGED")
	local r, g, b = self:hex2rgb(xhexcolor)
	self.currColor = r*65536 + g*256 + b
	local hpr = (1 - (r / 255)) * 51 -- 51 is image width 306 / 6 (#self.colors{} - 1)
	local hpg = (1 - (g / 255)) * 51
	local hpb = (1 - (b / 255)) * 51
	local hueposition = hpr + hpg + hpb
	self.hueArrows:setX(hueposition)
	self.e.color = self.currColor
	self:dispatchEvent(self.e)
end
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • olegoleg Member
    edited September 2019 Accepted Answer
    function rgb(r, g, b) return ((r*256)+g)*256+b end
    function Util:hex2rgb(hex)
        hex = hex:gsub("#","")
        return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))
    end

    Likes: MoKaLux

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 )Share on Facebook
  • keszeghkeszegh Member
    Accepted Answer
    it seems your color slider goes linearly from H0 S50 L50 to H359 S50 L50
    see
    https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Colors/Color_picker_tool
    so you need only to convert the rgb of the pixel to hsl first
    e.g.
    https://www.rapidtables.com/convert/color/rgb-to-hsl.html
    and then you can easily get the right place on the color slider. of course only if the color is present there, i.e., has S50 and L50. otherwise you can get the closest color by caring only about the H value.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited September 2019
    Thank you for your kind help. I am getting close:
    	local r, g, b = self:hex2rgb(xhexcolor)
    	local h, s, l = self:rgb2hsl(r, g, b)
    I now have the h value (between 0 and 360).
    One problem is left I cannot figure out how to transform this h value to translate to my image (306 pixel wide).

    edit: I think I get it: local hueposition = h * (306 / 360)

    Wonderful thank you very much guys.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • keszeghkeszegh Member
    Accepted Answer
    yes, although probably
    local hueposition = h * (305 / 359)
    is even more correct (assuming your colorslider has width 306)

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • @keszegh thank you our gfx specialist o:) thank you @oleg really appreciated!

    Likes: keszegh

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited September 2019 Accepted Answer
    I prefer to use this:
    function rgbToHex(r, g, b) 
        return (r << 16) + (g << 8) + b
    end
     
    function hexToRGB(hex)
        local r = hex >> 16
        local g = hex >> 8 & 0xFF
        local b = hex & 0xFF
        return r,g,b
    end
    And if you want to print/display hex number, just use
    string.format("0x%06x", hex)

    Likes: Apollo14, oleg

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.