|
| 1 | +local gpick = require('gpick') |
| 2 | +local color = require('gpick/color') |
| 3 | +local helpers = require('helpers') |
| 4 | +local _ = gpick._ |
| 5 | +local round = helpers.round |
| 6 | +local options = require('options') |
| 7 | +local serializeWebHex = function(colorObject) |
| 8 | + if not colorObject then return nil end |
| 9 | + local c = colorObject:getColor() |
| 10 | + if options.upperCase then |
| 11 | + return '#' .. string.format('%02X%02X%02X', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 12 | + else |
| 13 | + return '#' .. string.format('%02x%02x%02x', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 14 | + end |
| 15 | +end |
| 16 | +local deserializeWebHex = function(text, colorObject) |
| 17 | + local c = color:new() |
| 18 | + local findStart, findEnd, red, green, blue = string.find(text, '#([%x][%x])([%x][%x])([%x][%x])[^%x]?') |
| 19 | + if findStart ~= nil then |
| 20 | + red = tonumber(red, 16) |
| 21 | + green = tonumber(green, 16) |
| 22 | + blue = tonumber(blue, 16) |
| 23 | + c:red(red / 255) |
| 24 | + c:green(green / 255) |
| 25 | + c:blue(blue / 255) |
| 26 | + colorObject:setColor(c) |
| 27 | + return 1 - (math.atan(findStart - 1) / math.pi) - (math.atan(string.len(text) - findEnd) / math.pi) |
| 28 | + else |
| 29 | + return -1 |
| 30 | + end |
| 31 | +end |
| 32 | +local serializeWebHexNoHash = function(colorObject) |
| 33 | + if not colorObject then return nil end |
| 34 | + local c = colorObject:getColor() |
| 35 | + if options.upperCase then |
| 36 | + return string.format('%02X%02X%02X', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 37 | + else |
| 38 | + return string.format('%02x%02x%02x', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 39 | + end |
| 40 | +end |
| 41 | +local deserializeWebHexNoHash = function(text, colorObject) |
| 42 | + local c = color:new() |
| 43 | + local findStart, findEnd, red, green, blue = string.find(text, '([%x][%x])([%x][%x])([%x][%x])[^%x]?') |
| 44 | + if findStart ~= nil then |
| 45 | + red = tonumber(red, 16) |
| 46 | + green = tonumber(green, 16) |
| 47 | + blue = tonumber(blue, 16) |
| 48 | + c:red(red / 255) |
| 49 | + c:green(green / 255) |
| 50 | + c:blue(blue / 255) |
| 51 | + colorObject:setColor(c) |
| 52 | + return 1 - (math.atan(findStart - 1) / math.pi) - (math.atan(string.len(text) - findEnd) / math.pi) |
| 53 | + else |
| 54 | + return -1 |
| 55 | + end |
| 56 | +end |
| 57 | +local serializeWebHex3Digit = function(colorObject) |
| 58 | + if not colorObject then return nil end |
| 59 | + local c = colorObject:getColor() |
| 60 | + if options.upperCase then |
| 61 | + return '#' .. string.format('%01X%01X%01X', round(c:red() * 15), round(c:green() * 15), round(c:blue() * 15)) |
| 62 | + else |
| 63 | + return '#' .. string.format('%01x%01x%01x', round(c:red() * 15), round(c:green() * 15), round(c:blue() * 15)) |
| 64 | + end |
| 65 | +end |
| 66 | +local deserializeWebHex3Digit = function(text, colorObject) |
| 67 | + local c = color:new() |
| 68 | + local findStart, findEnd, red, green, blue = string.find(text, '#([%x])([%x])([%x])[^%x]?') |
| 69 | + if findStart ~= nil then |
| 70 | + red = tonumber(red, 16) |
| 71 | + green = tonumber(green, 16) |
| 72 | + blue = tonumber(blue, 16) |
| 73 | + c:red(red / 15) |
| 74 | + c:green(green / 15) |
| 75 | + c:blue(blue / 15) |
| 76 | + colorObject:setColor(c) |
| 77 | + return 1 - (math.atan(findStart - 1) / math.pi) - (math.atan(string.len(text) - findEnd) / math.pi) |
| 78 | + else |
| 79 | + return -1 |
| 80 | + end |
| 81 | +end |
| 82 | +local serializeCssHsl = function(colorObject) |
| 83 | + local c = colorObject:getColor() |
| 84 | + c = c:rgbToHsl() |
| 85 | + return 'hsl(' .. string.format('%d, %d%%, %d%%', round(c:hue() * 360), round(c:saturation() * 100), round(c:lightness() * 100)) .. ')' |
| 86 | +end |
| 87 | +local serializeCssRgb = function(colorObject) |
| 88 | + local c = colorObject:getColor() |
| 89 | + return 'rgb(' .. string.format('%d, %d, %d', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) .. ')' |
| 90 | +end |
| 91 | +local serializeColorCssBlock = function(colorObject, position) |
| 92 | + if not colorObject then return nil end |
| 93 | + local c = colorObject:getColor() |
| 94 | + local hsl = c:rgbToHsl() |
| 95 | + local result = ''; |
| 96 | + if position.first then |
| 97 | + result = '/**\n * Generated by Gpick ' .. gpick.version .. '\n' |
| 98 | + end |
| 99 | + local name = colorObject:getName() |
| 100 | + if not name then |
| 101 | + name = '' |
| 102 | + end |
| 103 | + result = result .. ' * ' .. name .. ': ' |
| 104 | + if options.upperCase then |
| 105 | + result = result .. '#' .. string.format('%02X%02X%02X', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 106 | + else |
| 107 | + result = result .. '#' .. string.format('%02x%02x%02x', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) |
| 108 | + end |
| 109 | + result = result .. ', rgb(' .. string.format('%d, %d, %d', round(c:red() * 255), round(c:green() * 255), round(c:blue() * 255)) .. '), hsl(' .. string.format('%d, %d%%, %d%%', round(c:hue() * 360), round(c:saturation() * 100), round(c:lightness() * 100)) .. ')' |
| 110 | + if position.last then |
| 111 | + result = result .. '\n */' |
| 112 | + end |
| 113 | + return result |
| 114 | +end |
| 115 | +local deserializeCssRgb = function(text, colorObject) |
| 116 | + local c = color:new() |
| 117 | + local findStart, findEnd, red, green, blue = string.find(text, 'rgb%(([%d]*)[%s]*,[%s]*([%d]*)[%s]*,[%s]*([%d]*)%)') |
| 118 | + if findStart ~= nil then |
| 119 | + c:rgb(math.min(1, red / 255), math.min(1, green / 255), math.min(1, blue / 255)) |
| 120 | + colorObject:setColor(c) |
| 121 | + return 1 - (math.atan(findStart - 1) / math.pi) - (math.atan(string.len(text) - findEnd) / math.pi) |
| 122 | + else |
| 123 | + return -1 |
| 124 | + end |
| 125 | +end |
| 126 | +local serializeCssColorHex = function(colorObject) |
| 127 | + return 'color: ' .. serializeWebHex(colorObject) |
| 128 | +end |
| 129 | +local serializeCssBackgroundColorHex = function(colorObject) |
| 130 | + return 'background-color: ' .. serializeWebHex(colorObject) |
| 131 | +end |
| 132 | +local serializeCssBorderColorHex = function(colorObject) |
| 133 | + return 'border-color: ' .. serializeWebHex(colorObject) |
| 134 | +end |
| 135 | +local serializeCssBorderTopColorHex = function(colorObject) |
| 136 | + return 'border-top-color: ' .. serializeWebHex(colorObject) |
| 137 | +end |
| 138 | +local serializeCssBorderRightColorHex = function(colorObject) |
| 139 | + return 'border-right-color: ' .. serializeWebHex(colorObject) |
| 140 | +end |
| 141 | +local serializeCssBorderBottomColorHex = function(colorObject) |
| 142 | + return 'border-bottom-color: ' .. serializeWebHex(colorObject) |
| 143 | +end |
| 144 | +local serializeCssBorderLeftHex = function(colorObject) |
| 145 | + return 'border-left-color: ' .. serializeWebHex(colorObject) |
| 146 | +end |
| 147 | +local serializeColorCsv = function(colorObject) |
| 148 | + local c = colorObject:getColor() |
| 149 | + os.setlocale("C", "numeric") |
| 150 | + local r = string.format('%f\t%f\t%f', c:red(), c:green(), c:blue()) |
| 151 | + os.setlocale("", "numeric") |
| 152 | + return r |
| 153 | +end |
| 154 | +gpick:addConverter('color_web_hex', _("Web: hex code"), serializeWebHex, deserializeWebHex) |
| 155 | +gpick:addConverter('color_web_hex_3_digit', _("Web: hex code (3 digits)"), serializeWebHex3Digit, deserializeWebHex3Digit) |
| 156 | +gpick:addConverter('color_web_hex_no_hash', _("Web: hex code (no hash symbol)"), serializeWebHexNoHash, deserializeWebHexNoHash) |
| 157 | +gpick:addConverter('color_css_hsl', _("CSS: hue saturation lightness"), serializeCssHsl) |
| 158 | +gpick:addConverter('color_css_rgb', _("CSS: red green blue"), serializeCssRgb, deserializeCssRgb) |
| 159 | +gpick:addConverter('css_color_hex', 'CSS(color)', serializeCssColorHex) |
| 160 | +gpick:addConverter('css_background_color_hex', 'CSS(background-color)', serializeCssBackgroundColorHex) |
| 161 | +gpick:addConverter('css_border_color_hex', 'CSS(border-color)', serializeCssBorderColorHex) |
| 162 | +gpick:addConverter('css_border_top_color_hex', 'CSS(border-top-color)', serializeCssBorderTopColorHex) |
| 163 | +gpick:addConverter('css_border_right_color_hex', 'CSS(border-right-color)', serializeCssBorderRightColorHex) |
| 164 | +gpick:addConverter('css_border_bottom_color_hex', 'CSS(border-bottom-color)', serializeCssBorderBottomColorHex) |
| 165 | +gpick:addConverter('css_border_left_hex', 'CSS(border-left-color)', serializeCssBorderLeftHex) |
| 166 | +gpick:addConverter('color_csv', 'CSV', serializeColorCsv) |
| 167 | +gpick:addConverter('color_css_block', 'CSS block', serializeColorCssBlock) |
| 168 | +return {} |
0 commit comments