2010-08-20 22:27:07 +02:00
|
|
|
---------------------------------------------------------------------------
|
2018-01-22 11:53:39 +01:00
|
|
|
-- This module simplifies the creation of cairo pattern objects.
|
|
|
|
--
|
|
|
|
-- In most places in awesome where a color is needed, the provided argument is
|
|
|
|
-- passed to @{gears.color}, which actually calls @{create_pattern} and creates
|
|
|
|
-- a pattern from a given string or table.
|
|
|
|
--
|
|
|
|
-- This function can create solid, linear, radial and png patterns.
|
|
|
|
--
|
|
|
|
-- A simple example for a solid pattern is a hexadecimal color specification.
|
|
|
|
-- For example `#ff8000` creates a solid pattern with 100% red, 50% green and 0%
|
|
|
|
-- blue. Limited support for named colors (`red`) is also provided.
|
|
|
|
--
|
|
|
|
-- In general, patterns are specified as strings formatted as
|
|
|
|
-- `"type:arguments"`. `"arguments"` is specific to the pattern being used. For
|
|
|
|
-- example, one can use:
|
|
|
|
-- "radial:50,50,10:55,55,30:0,#ff0000:0.5,#00ff00:1,#0000ff"
|
|
|
|
-- The above will call @{create_radial_pattern} with the provided string, after
|
|
|
|
-- stripping the `radial:` prefix.
|
|
|
|
--
|
|
|
|
-- Alternatively, patterns can be specified via tables. In this case, the
|
|
|
|
-- table's 'type' member specifies the type. For example:
|
|
|
|
-- {
|
|
|
|
-- type = "radial",
|
|
|
|
-- from = { 50, 50, 10 },
|
|
|
|
-- to = { 55, 55, 30 },
|
|
|
|
-- stops = { { 0, "#ff0000" }, { 0.5, "#00ff00" }, { 1, "#0000ff" } }
|
|
|
|
-- }
|
|
|
|
-- Any argument that cannot be understood is passed to @{create_solid_pattern}.
|
|
|
|
--
|
|
|
|
-- Please note that you MUST NOT modify the returned pattern, for example by
|
|
|
|
-- calling :set_matrix() on it, because this function uses a cache and your
|
|
|
|
-- changes could thus have unintended side effects. Use @{create_pattern_uncached}
|
|
|
|
-- if you need to modify the returned pattern.
|
|
|
|
--
|
2010-08-20 22:27:07 +02:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 09:49:21 +02:00
|
|
|
-- @themelib gears.color
|
2010-08-20 22:27:07 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local string = string
|
|
|
|
local table = table
|
2016-02-07 13:29:46 +01:00
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
2010-08-20 22:27:07 +02:00
|
|
|
local tonumber = tonumber
|
2012-02-17 17:48:11 +01:00
|
|
|
local ipairs = ipairs
|
2010-09-29 18:54:20 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
2016-01-17 14:27:29 +01:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local cairo = lgi.cairo
|
|
|
|
local Pango = lgi.Pango
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
2010-08-20 22:27:07 +02:00
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
local color = { mt = {} }
|
2015-06-13 16:55:10 +02:00
|
|
|
local pattern_cache
|
2021-12-11 18:29:27 +01:00
|
|
|
local color_string_cache = setmetatable({}, { __mode = "k" })
|
2010-08-20 22:27:07 +02:00
|
|
|
|
2010-09-29 18:54:20 +02:00
|
|
|
--- Parse a HTML-color.
|
2016-01-17 14:27:29 +01:00
|
|
|
-- This function can parse colors like `#rrggbb` and `#rrggbbaa` and also `red`.
|
2016-11-22 07:41:29 +01:00
|
|
|
-- Max 4 chars per channel.
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @param col The color to parse
|
2022-02-06 09:29:37 +01:00
|
|
|
-- @treturn number between 0 and 1 for the 'red' value (1st channel)
|
|
|
|
-- @treturn number between 0 and 1 for the 'green' value (2nd channel)
|
|
|
|
-- @treturn number between 0 and 1 for the 'blue' value (3rd channel)
|
|
|
|
-- @treturn number between 0 and 1 for the 'opacity' value (4th channel)
|
|
|
|
-- if the incoming color code only has 3 values (only rgb, not opacity) the 4th
|
|
|
|
-- return value is 1.
|
|
|
|
-- @treturn nil if input is incorrect
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.parse_color
|
2022-02-06 09:29:37 +01:00
|
|
|
-- @usage -- Both of the following will return 0, 0.4, 0.8, 1
|
|
|
|
-- gears.color.parse_color("#0066ccff")
|
|
|
|
-- gears.color.parse_color("#0066cc")
|
2012-06-12 10:13:46 +02:00
|
|
|
function color.parse_color(col)
|
2010-08-20 22:27:07 +02:00
|
|
|
local rgb = {}
|
2016-01-17 14:27:29 +01:00
|
|
|
if string.match(col, "^#%x+$") then
|
2016-11-20 09:40:56 +01:00
|
|
|
local hex_str = col:sub(2, #col)
|
|
|
|
local channels
|
|
|
|
if #hex_str % 3 == 0 then
|
|
|
|
channels = 3
|
|
|
|
elseif #hex_str % 4 == 0 then
|
|
|
|
channels = 4
|
|
|
|
else
|
|
|
|
return nil
|
2016-01-17 14:27:29 +01:00
|
|
|
end
|
2016-11-20 09:40:56 +01:00
|
|
|
local chars_per_channel = #hex_str / channels
|
|
|
|
if chars_per_channel > 4 then
|
|
|
|
return nil
|
|
|
|
end
|
2016-11-22 07:41:29 +01:00
|
|
|
local dividor = (0x10 ^ chars_per_channel) - 1
|
2016-11-20 09:40:56 +01:00
|
|
|
for idx=1,#hex_str,chars_per_channel do
|
|
|
|
local channel_val = tonumber(hex_str:sub(idx,idx+chars_per_channel-1), 16)
|
|
|
|
table.insert(rgb, channel_val / dividor)
|
|
|
|
end
|
|
|
|
if channels == 3 then
|
|
|
|
table.insert(rgb, 1)
|
2016-01-17 14:27:29 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
local c = Pango.Color()
|
2016-11-20 09:40:56 +01:00
|
|
|
if not c:parse(col) then
|
|
|
|
return nil
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
2016-11-20 09:40:56 +01:00
|
|
|
rgb = {
|
|
|
|
c.red / 0xffff,
|
|
|
|
c.green / 0xffff,
|
|
|
|
c.blue / 0xffff,
|
|
|
|
1.0
|
|
|
|
}
|
2010-08-20 22:27:07 +02:00
|
|
|
end
|
2016-11-20 09:40:56 +01:00
|
|
|
assert(#rgb == 4, col)
|
2010-08-20 22:27:07 +02:00
|
|
|
return unpack(rgb)
|
|
|
|
end
|
|
|
|
|
2010-09-29 18:54:20 +02:00
|
|
|
--- Find all numbers in a string
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
|
|
|
-- @tparam string s The string to parse
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @return Each number found as a separate value
|
|
|
|
local function parse_numbers(s)
|
|
|
|
local res = {}
|
|
|
|
for k in string.gmatch(s, "-?[0-9]+[.]?[0-9]*") do
|
|
|
|
table.insert(res, tonumber(k))
|
|
|
|
end
|
|
|
|
return unpack(res)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a solid pattern
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @param col The color for the pattern
|
|
|
|
-- @return A cairo pattern object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_solid_pattern
|
2012-06-12 10:13:46 +02:00
|
|
|
function color.create_solid_pattern(col)
|
2010-09-29 18:54:20 +02:00
|
|
|
if col == nil then
|
|
|
|
col = "#000000"
|
2012-02-17 17:48:11 +01:00
|
|
|
elseif type(col) == "table" then
|
|
|
|
col = col.color
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
2012-06-12 10:13:46 +02:00
|
|
|
return cairo.Pattern.create_rgba(color.parse_color(col))
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Create an image pattern from a png file
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @param file The filename of the file
|
|
|
|
-- @return a cairo pattern object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_png_pattern
|
2012-06-12 10:13:46 +02:00
|
|
|
function color.create_png_pattern(file)
|
2012-02-17 17:48:11 +01:00
|
|
|
if type(file) == "table" then
|
|
|
|
file = file.file
|
|
|
|
end
|
2012-05-27 19:20:34 +02:00
|
|
|
local image = surface.load(file)
|
2014-03-15 23:53:01 +01:00
|
|
|
local pattern = cairo.Pattern.create_for_surface(image)
|
|
|
|
pattern:set_extend(cairo.Extend.REPEAT)
|
|
|
|
return pattern
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Add stops to the given pattern.
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @param p The cairo pattern to add stops to
|
|
|
|
-- @param iterator An iterator that returns strings. Each of those strings
|
2014-05-19 15:15:39 +02:00
|
|
|
-- should be in the form place,color where place is in [0, 1].
|
2012-02-17 17:48:11 +01:00
|
|
|
local function add_iterator_stops(p, iterator)
|
2010-09-29 18:54:20 +02:00
|
|
|
for k in iterator do
|
|
|
|
local sub = string.gmatch(k, "[^,]+")
|
2012-06-12 10:13:46 +02:00
|
|
|
local point, clr = sub(), sub()
|
|
|
|
p:add_color_stop_rgba(point, color.parse_color(clr))
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Add a list of stops to a given pattern
|
2012-02-17 17:48:11 +01:00
|
|
|
local function add_stops_table(pat, arg)
|
|
|
|
for _, stop in ipairs(arg) do
|
2012-06-12 10:13:46 +02:00
|
|
|
pat:add_color_stop_rgba(stop[1], color.parse_color(stop[2]))
|
2012-02-17 17:48:11 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Create a pattern from a string
|
2012-02-17 17:48:11 +01:00
|
|
|
local function string_pattern(creator, arg)
|
|
|
|
local iterator = string.gmatch(arg, "[^:]+")
|
|
|
|
-- Create a table where each entry is a number from the original string
|
|
|
|
local args = { parse_numbers(iterator()) }
|
|
|
|
local to = { parse_numbers(iterator()) }
|
|
|
|
-- Now merge those two tables
|
2016-02-07 13:29:46 +01:00
|
|
|
for _, v in pairs(to) do
|
2012-02-17 17:48:11 +01:00
|
|
|
table.insert(args, v)
|
|
|
|
end
|
|
|
|
-- And call our creator function with the values
|
2012-02-26 05:09:09 +01:00
|
|
|
local p = creator(unpack(args))
|
2012-02-17 17:48:11 +01:00
|
|
|
|
|
|
|
add_iterator_stops(p, iterator)
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2010-09-29 18:54:20 +02:00
|
|
|
--- Create a linear pattern object.
|
|
|
|
-- The pattern is created from a string. This string should have the following
|
2014-05-19 13:36:38 +02:00
|
|
|
-- form: `"x0, y0:x1, y1:<stops>"`
|
2012-02-17 17:48:11 +01:00
|
|
|
-- Alternatively, the pattern can be specified as a table:
|
2014-05-19 15:15:39 +02:00
|
|
|
-- { type = "linear", from = { x0, y0 }, to = { x1, y1 },
|
|
|
|
-- stops = { <stops> } }
|
2014-05-19 13:36:38 +02:00
|
|
|
-- `x0,y0` and `x1,y1` are the start and stop point of the pattern.
|
|
|
|
-- For the explanation of `<stops>`, see `color.create_pattern`.
|
|
|
|
-- @tparam string|table arg The argument describing the pattern.
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @return a cairo pattern object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_linear_pattern
|
2012-06-12 10:13:46 +02:00
|
|
|
function color.create_linear_pattern(arg)
|
2012-02-17 17:48:11 +01:00
|
|
|
local pat
|
|
|
|
|
|
|
|
if type(arg) == "string" then
|
2012-05-27 19:20:34 +02:00
|
|
|
return string_pattern(cairo.Pattern.create_linear, arg)
|
2012-02-17 17:48:11 +01:00
|
|
|
elseif type(arg) ~= "table" then
|
|
|
|
error("Wrong argument type: " .. type(arg))
|
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
pat = cairo.Pattern.create_linear(arg.from[1], arg.from[2], arg.to[1], arg.to[2])
|
2012-02-17 17:48:11 +01:00
|
|
|
add_stops_table(pat, arg.stops)
|
|
|
|
return pat
|
|
|
|
end
|
2010-09-29 18:54:20 +02:00
|
|
|
|
|
|
|
--- Create a radial pattern object.
|
|
|
|
-- The pattern is created from a string. This string should have the following
|
2014-05-19 13:36:38 +02:00
|
|
|
-- form: `"x0, y0, r0:x1, y1, r1:<stops>"`
|
2012-02-17 17:48:11 +01:00
|
|
|
-- Alternatively, the pattern can be specified as a table:
|
2014-05-19 15:15:39 +02:00
|
|
|
-- { type = "radial", from = { x0, y0, r0 }, to = { x1, y1, r1 },
|
|
|
|
-- stops = { <stops> } }
|
2014-05-19 13:36:38 +02:00
|
|
|
-- `x0,y0` and `x1,y1` are the start and stop point of the pattern.
|
|
|
|
-- `r0` and `r1` are the radii of the start / stop circle.
|
|
|
|
-- For the explanation of `<stops>`, see `color.create_pattern`.
|
|
|
|
-- @tparam string|table arg The argument describing the pattern
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @return a cairo pattern object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_radial_pattern
|
2012-06-12 10:13:46 +02:00
|
|
|
function color.create_radial_pattern(arg)
|
2012-02-17 17:48:11 +01:00
|
|
|
local pat
|
2010-09-29 18:54:20 +02:00
|
|
|
|
2012-02-17 17:48:11 +01:00
|
|
|
if type(arg) == "string" then
|
2012-05-27 19:20:34 +02:00
|
|
|
return string_pattern(cairo.Pattern.create_radial, arg)
|
2012-02-17 17:48:11 +01:00
|
|
|
elseif type(arg) ~= "table" then
|
|
|
|
error("Wrong argument type: " .. type(arg))
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
2012-02-17 17:48:11 +01:00
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
pat = cairo.Pattern.create_radial(arg.from[1], arg.from[2], arg.from[3],
|
2012-02-17 17:48:11 +01:00
|
|
|
arg.to[1], arg.to[2], arg.to[3])
|
|
|
|
add_stops_table(pat, arg.stops)
|
|
|
|
return pat
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
|
|
|
|
2012-02-17 17:48:11 +01:00
|
|
|
--- Mapping of all supported color types. New entries can be added.
|
2012-06-12 10:13:46 +02:00
|
|
|
color.types = {
|
2012-06-12 10:13:46 +02:00
|
|
|
solid = color.create_solid_pattern,
|
|
|
|
png = color.create_png_pattern,
|
|
|
|
linear = color.create_linear_pattern,
|
|
|
|
radial = color.create_radial_pattern
|
2010-09-29 18:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--- Create a pattern from a given string.
|
2014-05-19 13:36:38 +02:00
|
|
|
-- For full documentation of this function, please refer to
|
|
|
|
-- `color.create_pattern`. The difference between `color.create_pattern`
|
|
|
|
-- and this function is that this function does not insert the generated
|
|
|
|
-- objects into the pattern cache. Thus, you are allowed to modify the
|
|
|
|
-- returned object.
|
2014-09-13 14:16:54 +02:00
|
|
|
-- @see create_pattern
|
2010-09-29 18:54:20 +02:00
|
|
|
-- @param col The string describing the pattern.
|
|
|
|
-- @return a cairo pattern object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_pattern_uncached
|
2014-09-13 14:16:54 +02:00
|
|
|
function color.create_pattern_uncached(col)
|
2013-02-11 14:26:05 +01:00
|
|
|
-- If it already is a cairo pattern, just leave it as that
|
|
|
|
if cairo.Pattern:is_type_of(col) then
|
|
|
|
return col
|
|
|
|
end
|
2016-02-07 13:29:46 +01:00
|
|
|
col = col or "#000000"
|
2010-09-29 18:54:20 +02:00
|
|
|
if type(col) == "string" then
|
|
|
|
local t = string.match(col, "[^:]+")
|
2012-06-12 10:13:46 +02:00
|
|
|
if color.types[t] then
|
2010-09-29 18:54:20 +02:00
|
|
|
local pos = string.len(t)
|
|
|
|
local arg = string.sub(col, pos + 2)
|
2014-09-13 14:16:54 +02:00
|
|
|
return color.types[t](arg)
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
2012-02-17 17:48:11 +01:00
|
|
|
elseif type(col) == "table" then
|
|
|
|
local t = col.type
|
2012-06-12 10:13:46 +02:00
|
|
|
if color.types[t] then
|
2014-09-13 14:16:54 +02:00
|
|
|
return color.types[t](col)
|
2012-02-17 17:48:11 +01:00
|
|
|
end
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
2014-09-13 14:16:54 +02:00
|
|
|
return color.create_solid_pattern(col)
|
|
|
|
end
|
|
|
|
|
2018-01-22 11:53:39 +01:00
|
|
|
--- Create a pattern from a given string, same as @{gears.color}.
|
2016-11-22 07:41:29 +01:00
|
|
|
-- @see gears.color
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @treturn gears.color A cairo pattern object.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_pattern
|
2014-09-13 14:16:54 +02:00
|
|
|
function color.create_pattern(col)
|
|
|
|
if cairo.Pattern:is_type_of(col) then
|
|
|
|
return col
|
|
|
|
end
|
2015-06-13 16:55:10 +02:00
|
|
|
return pattern_cache:get(col or "#000000")
|
2010-09-29 18:54:20 +02:00
|
|
|
end
|
|
|
|
|
2014-03-16 14:58:14 +01:00
|
|
|
--- Check if a pattern is opaque.
|
|
|
|
-- A pattern is transparent if the background on which it gets drawn (with
|
|
|
|
-- operator OVER) doesn't influence the visual result.
|
2014-05-19 15:15:39 +02:00
|
|
|
-- @param col An argument that `create_pattern` accepts.
|
2014-03-16 14:58:14 +01:00
|
|
|
-- @return The pattern if it is surely opaque, else nil
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.create_opaque_pattern
|
2014-03-16 14:58:14 +01:00
|
|
|
function color.create_opaque_pattern(col)
|
|
|
|
local pattern = color.create_pattern(col)
|
2016-02-07 13:29:46 +01:00
|
|
|
local kind = pattern:get_type()
|
2014-03-16 14:58:14 +01:00
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
if kind == "SOLID" then
|
|
|
|
local _, _, _, _, alpha = pattern:get_rgba()
|
|
|
|
if alpha ~= 1 then
|
2014-03-16 14:58:14 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
return pattern
|
2016-02-07 13:29:46 +01:00
|
|
|
elseif kind == "SURFACE" then
|
|
|
|
local status, surf = pattern:get_surface()
|
|
|
|
if status ~= "SUCCESS" or surf.content ~= "COLOR" then
|
2014-03-16 14:58:14 +01:00
|
|
|
-- The surface has an alpha channel which *might* be non-opaque
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Only the "NONE" extend mode is forbidden, everything else doesn't
|
|
|
|
-- introduce transparent parts
|
|
|
|
if pattern:get_extend() == "NONE" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return pattern
|
2016-02-07 13:29:46 +01:00
|
|
|
elseif kind == "LINEAR" then
|
|
|
|
local _, stops = pattern:get_color_stop_count()
|
2014-03-16 14:58:14 +01:00
|
|
|
|
|
|
|
-- No color stops or extend NONE -> pattern *might* contain transparency
|
|
|
|
if stops == 0 or pattern:get_extend() == "NONE" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Now check if any of the color stops contain transparency
|
|
|
|
for i = 0, stops - 1 do
|
2016-02-07 13:29:46 +01:00
|
|
|
local _, _, _, _, _, alpha = pattern:get_color_stop_rgba(i)
|
|
|
|
if alpha ~= 1 then
|
2014-03-16 14:58:14 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return pattern
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Unknown type, e.g. mesh or raster source or unsupported type (radial
|
|
|
|
-- gradients can do weird self-intersections)
|
|
|
|
end
|
|
|
|
|
2015-08-06 18:54:06 +02:00
|
|
|
--- Fill non-transparent area of an image with a given color.
|
|
|
|
-- @param image Image or path to it.
|
|
|
|
-- @param new_color New color.
|
|
|
|
-- @return Recolored image.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.recolor_image
|
2015-08-02 12:46:44 +02:00
|
|
|
function color.recolor_image(image, new_color)
|
2021-03-28 08:48:48 +02:00
|
|
|
image = surface.duplicate_surface(image)
|
2015-08-02 12:46:44 +02:00
|
|
|
local cr = cairo.Context.create(image)
|
|
|
|
cr:set_source(color.create_pattern(new_color))
|
|
|
|
cr:mask(cairo.Pattern.create_for_surface(image), 0, 0)
|
|
|
|
return image
|
|
|
|
end
|
|
|
|
|
2021-12-13 18:49:00 +01:00
|
|
|
--- Take an input color and set a different opacity.
|
|
|
|
--
|
|
|
|
-- @staticfct gears.color.change_opacity
|
|
|
|
-- @tparam string|pattern input The input color.
|
|
|
|
-- @tparam number opacity A floating point number between 0 and 1.
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @treturn color The new color if successful or `input` if invalid.
|
2021-12-13 18:49:00 +01:00
|
|
|
function color.change_opacity(input, opacity)
|
|
|
|
input = color.create_pattern(input)
|
|
|
|
|
|
|
|
local error, r, g, b, _ = input:get_rgba()
|
|
|
|
|
|
|
|
if error ~= "SUCCESS" then return input end
|
|
|
|
|
|
|
|
return cairo.Pattern.create_rgba(r, g, b, opacity)
|
|
|
|
end
|
|
|
|
|
2021-12-11 18:29:27 +01:00
|
|
|
--- Convert a color back to an hexadecimal color code.
|
|
|
|
--
|
|
|
|
-- This takes an input color, pattern or gradient and attempt to convert it
|
|
|
|
-- to a color. If this fails, `fallback` is returned. This is useful when a
|
|
|
|
-- color needs to be concatenated into a Pango markup string.
|
|
|
|
--
|
|
|
|
-- @staticfct gears.color.to_rgba_string
|
|
|
|
-- @tparam pattern|string|gradient color Note that only solid colors can be
|
|
|
|
-- convedted to the `RGBA` format.
|
|
|
|
-- @tparam[opt=nil] pattern|string|gradient fallback The color to return
|
|
|
|
-- if `color` cannot be converted to a string.
|
|
|
|
-- @treturn string The color in `#rrggbbaa` format.
|
|
|
|
-- @see gears.color.ensure_pango_color
|
|
|
|
|
|
|
|
function color.to_rgba_string(col, fallback)
|
|
|
|
if (not col) and (not fallback) then return nil end
|
|
|
|
|
|
|
|
-- Prevent infinite recursion.
|
|
|
|
if not col then return color.to_rgba_string(fallback) end
|
|
|
|
|
|
|
|
if color_string_cache[col] then
|
|
|
|
return color_string_cache[col]
|
|
|
|
end
|
|
|
|
|
|
|
|
col = color.create_pattern(col)
|
|
|
|
|
|
|
|
local error1, error2, r, g, b, a = pcall(function () return col:get_rgba() end)
|
|
|
|
|
|
|
|
-- Surface patterns don't have an RGBA representation.
|
|
|
|
if (not error1) or error2 ~= "SUCCESS" then return color.to_rgba_string(fallback) end
|
|
|
|
|
|
|
|
color_string_cache[col] = string.format(
|
|
|
|
"#%02x%02x%02x%02x",
|
|
|
|
math.floor(r*255),
|
|
|
|
math.floor(g*255),
|
|
|
|
math.floor(b*255),
|
|
|
|
math.floor(a*255)
|
|
|
|
)
|
|
|
|
|
|
|
|
return color_string_cache[col]
|
|
|
|
end
|
|
|
|
|
2017-03-03 00:02:03 +01:00
|
|
|
--- Get a valid color for Pango markup
|
2017-03-12 11:45:41 +01:00
|
|
|
-- @param check_color The color to check.
|
2017-03-03 00:02:03 +01:00
|
|
|
-- @tparam string fallback The color to return if the first is invalid. (default: black)
|
|
|
|
-- @treturn string color if it is valid, else fallback.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.color.ensure_pango_color
|
2017-03-03 00:02:03 +01:00
|
|
|
function color.ensure_pango_color(check_color, fallback)
|
2021-12-11 18:29:27 +01:00
|
|
|
-- This will happen if `gears.color` has been called in the theme.
|
|
|
|
if type(check_color) == "userdata" then
|
|
|
|
return color.to_rgba_string(check_color, fallback)
|
|
|
|
end
|
|
|
|
|
2017-03-03 00:02:03 +01:00
|
|
|
check_color = tostring(check_color)
|
2017-12-07 10:47:58 +01:00
|
|
|
-- Pango markup supports alpha, PangoColor does not. Thus, check for this.
|
|
|
|
local len = #check_color
|
|
|
|
if string.match(check_color, "^#%x+$") and (len == 5 or len == 9 or len == 17) then
|
|
|
|
return check_color
|
|
|
|
end
|
2017-03-03 00:02:03 +01:00
|
|
|
return Pango.Color.parse(Pango.Color(), check_color) and check_color or fallback or "black"
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
function color.mt.__call(_, ...)
|
2012-06-12 10:13:46 +02:00
|
|
|
return color.create_pattern(...)
|
|
|
|
end
|
|
|
|
|
2015-06-13 16:55:10 +02:00
|
|
|
pattern_cache = require("gears.cache").new(color.create_pattern_uncached)
|
|
|
|
|
2016-03-04 09:16:56 +01:00
|
|
|
--- No color
|
|
|
|
color.transparent = color.create_pattern("#00000000")
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
return setmetatable(color, color.mt)
|
2010-08-20 22:27:07 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|