|
| 1 | +--- |
| 2 | +title: Colorizer |
| 3 | +--- |
| 4 | + |
| 5 | +# About |
| 6 | +`colorizer` is a setting which allows you to recolor the pixels in the output image after it has been generated. |
| 7 | + |
| 8 | +You can do this by setting `colorizer` to either a function or a table. |
| 9 | +If you choose a function, that function will be run for every pixel in the output image. This might be what you want! But it can also be a bit slow. Choosing table allows you to create direct color-to-color conversions, which are much faster to execute. You are still able to choose if a color should be recolored using a function, so unless necessary, I would go for the table version. |
| 10 | + |
| 11 | +Whether you write one main function, or a function that only operates on one color, it will need to follow this spec: |
| 12 | +- *Input*: one parameter, a [`PixelInfo` object](/userdata/pixelinfo) |
| 13 | +- *Output*: one of: |
| 14 | + - a hex code, like `"#FF0000"` or `"#FF0000FF"` |
| 15 | + - a table with `r`, `g`, `b`, and `a` values. `a` is optional, and will default to `255` |
| 16 | +::: details Example |
| 17 | +```lua |
| 18 | +function (info) |
| 19 | + -- interchanges the r, g, and b values |
| 20 | + return { |
| 21 | + r = info.g, |
| 22 | + g = info.b, |
| 23 | + b = info.r, |
| 24 | + a = info.a |
| 25 | + } |
| 26 | +end |
| 27 | +``` |
| 28 | +::: |
| 29 | + |
| 30 | +## Ways to set |
| 31 | +### Table |
| 32 | +A `colorizer` table is a mapping from input color codes -- the left side of the equals sign -- to either another color code, or a function which follows the spec above. `colorizer.default` is treated specially: if this is set, any pixel which doesn't match a color code is fed to the `default`. This is probably most useful as a function, but you can set it to a color code as well if you like. |
| 33 | + |
| 34 | +::: details Example |
| 35 | +```lua |
| 36 | +colorizer = { |
| 37 | + ["#000000"] = "#00FF00", |
| 38 | + ["#FF0000"] = function (info) |
| 39 | + -- function logic omitted |
| 40 | + return hex_code |
| 41 | + end, |
| 42 | + |
| 43 | + default = function (info) |
| 44 | + -- function logic omitted |
| 45 | + return hex_code |
| 46 | + end |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Here black pixels (`"#000000"`) will get mapped to green (`"#00FF00"`), red pixels (`"#FF0000"`) will be set to the result of a function, and everything else will be set to the result of a second function. |
| 51 | +::: |
| 52 | +::: details Tips and Info |
| 53 | +- The fields have to be set using the weird-looking `["#000000"]` because `#` means something in actual lua code. Doing it this way wraps it in a string. |
| 54 | +- If you have a color-code variable, perhaps imported from something like a `colorscheme.lua`, and you want to use it on the left side of the `=`, make sure to wrap it in brackets as well. This is not necessary on the right side. A la |
| 55 | +```lua |
| 56 | +local my_variable = "#00FF00" |
| 57 | +local other_var = "#0000FF" |
| 58 | + |
| 59 | +colorizer = { |
| 60 | + [my_variable] = other_var |
| 61 | +} |
| 62 | +``` |
| 63 | +Otherwise Wavewall will attempt to convert `"my_variable"` to a color and throw an error |
| 64 | +- You can include alpha values on both sides as well, for instance `"#FF0000FF"` |
| 65 | + |
| 66 | +::: |
| 67 | +### Function |
| 68 | +See the example used in the function spec above. Setting `colorizer` this way is essentially the same as setting `colorizer` to a table with only `default` defined. |
| 69 | + |
0 commit comments