Advertisement
Guest User

[ROBLOX] HEX Format

a guest
Feb 17th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. --[[
  2.     DOCUMENTATION:
  3.     All types of HEX Converter on ROBLOX in one place.
  4. --]]
  5.  
  6. local function GetRGBFromData(Color)
  7.     return {(Color.R * 255), (Color.G * 255), (Color.B * 255)} -- {(Color.R), (Color.G), (Color.B)}
  8. end
  9.  
  10. local function InvertHexColor(Hex)
  11.     Hex = string.upper(Hex)
  12.     Hex = string.sub(Hex, 2)
  13.     local InvertedHex = "#" -- "0x"
  14.  
  15.     for Index = 0, 2 do
  16.         local Component = string.sub(Hex, (1 + Index * 2), (2 + Index * 2) )
  17.         Component = tonumber("0x" .. Component)
  18.         Component = (0xff - Component)
  19.         InvertedHex = (InvertedHex .. string.format("%.2x", Component) )
  20.     end
  21.    
  22.     return string.upper(InvertedHex)
  23. end
  24.  
  25. local function ConvertRGBToHex(RGB)
  26.     local Hexadecimal = "#" -- "0x"
  27.    
  28.     for _, Value in pairs(RGB) do
  29.         local Hex = ""
  30.        
  31.         while Value > 0 do
  32.             local Index = (math.fmod(Value, 16) + 1)
  33.             Value = math.floor(Value / 16)
  34.             Hex = (string.sub("0123456789ABCDEF", Index, Index) .. Hex)    
  35.         end
  36.        
  37.         if string.len(Hex) == 0 then
  38.             Hex = "00"
  39.         elseif string.len(Hex) == 1 then
  40.             Hex = ("0" .. Hex)
  41.         end
  42.        
  43.         Hexadecimal = (Hexadecimal .. Hex)
  44.     end
  45.    
  46.     return Hexadecimal
  47. end
  48.  
  49. local function ConvertHexToRGB(Hex)
  50.     Hex = Hex:gsub("#", "") -- Hex:gsub("0x", "")
  51.     local R = (tonumber("0x" .. Hex:sub(1, 2) ) * 255) -- tonumber("0x" .. Hex:sub(1, 2) )
  52.     local G = (tonumber("0x" .. Hex:sub(3, 4) ) * 255) -- tonumber("0x" .. Hex:sub(1, 2) )
  53.     local B = (tonumber("0x" .. Hex:sub(5, 6) ) * 255) -- tonumber("0x" .. Hex:sub(1, 2) )
  54.    
  55.     return R, G, B
  56. end
  57.  
  58. print( ConvertRGBToHex({255, 255, 255}) ) -- Output: #FFFFFF
  59. print( Color3.fromRGB(ConvertHexToRGB("#FFFFFF") ) ) -- Output: 255, 255, 255
  60. print( InvertHexColor("#FFFFFF") ) -- Output: #000000
  61.  
  62. print( Color3.fromRGB(ConvertHexToRGB(InvertHexColor("#FFFFFF") ) ) ) -- Output: 0, 0, 0
  63. print( ConvertRGBToHex(GetRGBFromData(workspace.Baseplate.Color) ) ) -- Output: #635F62
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement