Change RGB from int in [0,255] to float in [0,1]

Scott Yeskie [05-23-14 - 01:34]
Change RGB from int in [0,255] to float in [0,1]
Filename
GuildCharNames.lua
diff --git a/GuildCharNames.lua b/GuildCharNames.lua
index aa4271a..37444a3 100644
--- a/GuildCharNames.lua
+++ b/GuildCharNames.lua
@@ -172,7 +172,7 @@ end
     @param channel - chat channel code
     @param acctName - sender of chat message
     @return formatted name link [Character] (Main) [*]
- ]]
+]]--
 GuildCharInfo.getFormattedNameLink = function(channel, acctName, _)
     local ChanInfoArray = ZO_ChatSystem_GetChannelInfo()
     local info = ChanInfoArray[channel]
@@ -233,7 +233,7 @@ GuildCharInfo.classLookup[6] = "templar"
     @param varargs - string concatenation of args.
         Format: accountName,guildID,guildIndex
     @post shows UI box with player info
- ]]
+]]--
 GuildCharInfo.linkHandler = function(_, _, _, _, linkType, varargs)
 	if linkType ~= "playerDetail" then return false end

@@ -271,14 +271,14 @@ local function n0(val) if val == nil then return 0 else return val end end
         Note: if not length 6, wil return NIL values
     @return 3 values: red, green, blue
         All format decimal of range [0,1]
- ]]
+]]--
 GuildCharInfo.hex2rgb = function(hex)
     --Return nil on invalid input
     if #hex ~= 6 then return nil, nil, nil end
     --Convert each two digits to a number
     local r= GuildCharInfo.hex2dec(string.sub(hex, 1, 2))
-    local g= GuildCharInfo.hex2dec(string.sub(hex, 3, 2))
-    local b= GuildCharInfo.hex2dec(string.sub(hex, 5, 2))
+    local g= GuildCharInfo.hex2dec(string.sub(hex, 3, 4))
+    local b= GuildCharInfo.hex2dec(string.sub(hex, 5, 6))
     return n0(r)/255,n0(g)/255,n0(b)/255
 end

@@ -290,15 +290,18 @@ end
     @return 6-digit hex string in range 000000 to FFFFFF
  ]]
 GuildCharInfo.rgb2hex = function(r, g, b)
-    --Clip bounds
-    if r > 1 then r = 1 end
-    if g > 1 then g = 1 end
-    if b > 1 then b = 1 end
-    if r < 0 then r = 0 end
-    if g < 0 then g = 0 end
-    if b < 0 then b = 0 end
-
-    return string.format("%X%X%X",math.floor(r*255),math.floor(g*255),math.floor(b*255))
+    local str = GuildCharInfo.num2hex2(r)
+    str = str .. GuildCharInfo.num2hex2(g)
+    str = str .. GuildCharInfo.num2hex2(b)
+    return str
+end
+
+GuildCharInfo.num2hex2 = function(d)
+    if d > 1 then d = 1 end
+    if d < 0 then d = 0 end
+    local str = string.format("%X", d*255)
+    if #str == 1 then str = "0" .. str end
+    return str
 end

 --Quick hex2dec taking advantage of the fact tonumber recognizes hex input
@@ -311,7 +314,7 @@ end
     Init function to create UI configuration menu and managed config variables
     @post - UI menu created
     @post - preferences created and saved
- ]]
+]]--
 GuildCharInfo.initUI = function()
     --Defaults
     local cfgDefault = {}