Why the same value of color (r, g, b) and hex color make output different

When I use set Color in Corona:

local color = require("convertcolor") local line = display.newLine(100, 100, 200, 0); line:setColor(255, 0, 0); line.width = 3; line:append(300, 100, 100, 100); local rect = display.newRect(200, 200, 200, 200); rect.strokeWidth = 3; rect:setFillColor(200, 0, 0); rect:setStrokeColor(0, 200, 200); local rect1 = display.newRect(270, 265, 50, 70); rect.strokeWidth = 3; rect:setFillColor(0, 255, 0); rect:setStrokeColor(255, 255, 255); local roundRect = display.newRoundedRect(200, 500, 100, 100, 20); -- --------------------- THE SAME VALUE COLOR ----------------------------- --roundRect:setFillColor(color.hex('663300')); roundRect:setFillColor(102, 51, 0);

and it make output:

 before.png

     (Wrong Color)

but when I use other function I found on Internet:

roundRect:setFillColor(color.hex('663300'));

it make output:  -> right color

-It’s file convertcolor.lua I found:

local M = {} local function hex (hex) local redColor,greenColor,blueColor=hex:match('(..)(..)(..)') redColor, greenColor, blueColor = tonumber(redColor, 16)/255, tonumber(greenColor, 16)/255, tonumber(blueColor, 16)/255 redColor, greenColor, blueColor = math.floor(redColor\*100)/100, math.floor(greenColor\*100)/100, math.floor(blueColor\*100)/100 return redColor, greenColor, blueColor end local function rgb (r, g, b) local redColor,greenColor,blueColor=r/255, g/255, b/255 redColor, greenColor, blueColor = math.floor(redColor\*100)/100, math.floor(greenColor\*100)/100, math.floor(blueColor\*100)/100 return redColor, greenColor, blueColor end M.hex = hex M.rgb = rgb return M

Can someone explain for me my case? Thank so much!

First, Corona switched to using colors in the range of 0…1 a couple of years ago. We haven’t used 0…255 in a long time. It’s working for you for primary colors. Since 255 is > 1, its treated as if you used 1. Your line:

roundRect:setFillColor(102, 51, 0);

is in effect:

roundRect:setFillColor(1, 1, 0);

which is producing a Yellow rectangle. But the color converter is properly producing 0…1 colors which is why you’re getting the brown color.

First, Corona switched to using colors in the range of 0…1 a couple of years ago. We haven’t used 0…255 in a long time. It’s working for you for primary colors. Since 255 is > 1, its treated as if you used 1. Your line:

roundRect:setFillColor(102, 51, 0);

is in effect:

roundRect:setFillColor(1, 1, 0);

which is producing a Yellow rectangle. But the color converter is properly producing 0…1 colors which is why you’re getting the brown color.