--[[
作者 : Reyn
備注 : 色值格式化、轉換和定義
]]
----------------------------------------------------------------------------------------------
local strfmt = string.format
local strsub = string.sub
----------------------------------------------------------------------------------------------
--[[doc: 通道默認數值]]
local channel_default = 255
--[[doc: 24位默認十六進制字符串]]
local hex24_default = '#FFFFFF'
--[[doc: 32位默認十六進制字符串]]
local hex32_default = '#FFFFFFFF'
--[[doc: 24位默認RGB色值]]
local rgb_default = {
r = channel_default,
g = channel_default,
b = channel_default
}
--[[doc: 32位默認RGB色值]]
local rgba_default = {
r = rgb_default.r,
g = rgb_default.g,
b = rgb_default.b,
a = channel_default
}
--[[func: 轉換為可用的色位]]
local function to_color_bit( color )
color = tonumber(color)
if color and color <= channel_default and channel_default >= 0 then
return color
end
return channel_default
end
--[[func: 轉換為有效的色值]]
local function to_valid_color( color )
if type(color) ~= 'table' then
return rgba_default
end
color.r = to_color_bit( color.r )
color.g = to_color_bit( color.g )
color.b = to_color_bit( color.b )
color.a = to_color_bit( color.a )
end
--[[func: 從十六進制字符串轉為色位]]
local function str_to_hex_num( hex )
return tonumber( hex, 16 ) or channel_default
end
--[[func: 數值轉換為十六進制字符串]]
local function num_to_hex_str( value )
return strfmt( '%02X', to_color_bit( value ) )
end
--[[func: 從十六進制字符串轉換]]
local function hex_to_color( hex )
local len = string.len( hex )
if len < 7 then
return rgb_default
end
if strsub( hex, 1, 1 ) ~= '#' then
return rgb_default
end
local color = {
r = to_color_bit ( str_to_hex_num( strsub( hex, 2, 3 ) ) ),
g = to_color_bit ( str_to_hex_num( strsub( hex, 4, 5 ) ) ),
b = to_color_bit ( str_to_hex_num( strsub( hex, 6, 7 ) ) ),
a = channel_default
}
if len == 9 then
color.a = to_color_bit( str_to_hex_num( strsub( hex, 8, 9 ) ) )
end
return color
end
--[[func: 從數值表轉換]]
local function tab_to_color( tbl )
return {
r = to_color_bit( tbl[1] ),
g = to_color_bit( tbl[2] ),
b = to_color_bit( tbl[3] ),
a = to_color_bit( tbl[4] )
}
end
--[[func: 轉為為數值表]]
local function color_to_tab( color )
to_valid_color( color )
return { color.r , color.g, color.b, color.a }
end
--[[func: 轉換為十六進制(24位)]]
local function color_to_hex24( color )
if type( color ) ~= 'table' then
return hex24_default
end
if not color.r or not color.g or not color.b then
return hex24_default
end
local hex = '#'
hex = hex .. num_to_hex_str( color.r )
hex = hex .. num_to_hex_str( color.g )
hex = hex .. num_to_hex_str( color.b )
return hex
end
--[[func: 轉換為十六進制(32位)]]
local function color_to_hex32( color )
return color_to_hex24( color ) .. num_to_hex_str( color.a )
end
----------------------------------------------------------------------------------------------
return {
--[[doc: 方法定義]]
FromHex = hex_to_color,
FromTab = tab_to_color,
ToTab = color_to_tab,
ToHex24 = color_to_hex24,
ToHex32 = color_to_hex32,
ToHex = color_to_hex24,
--[[doc: 色值定義]]
Hex = {
--[[doc: 紅色 光的三原色之一]]
Red = '#FF0000',
--[[doc: 綠色 光的三原色之一]]
Green = '#00FF00',
--[[doc: 藍色 光的三原色之一]]
Blue = '#0000FF',
--[[doc: 白色 等量的三原色光相加為白光]]
White = '#FFFFFF',
--[[doc: 黃色 顏料三原色之一]]
Yellow = '#FFFF00',
--[[doc: 品紅 顏料三原色之一]]
Magenta = '#FF00FF',
--[[doc: 青色 顏料三原色之一 與 Cyan 異名同色]]
Aqua = '#00FFFF',
Cyan = '#00FFFF',
--[[doc: 黑色 顏料三原色混合為黑色]]
Black = '#000000',
}
}
Color.lua
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內容
- Xamarin XAML語言教程構建ControlTemplate控件模板 控件模板ControlTemplate...
- 最近開發(fā)微信小程序,遇到到過種坑,例如什么前端發(fā)送請求,后端請求成功,沒有獲取到前端傳遞的值;后端請求成功...