Due to my template requirements, I need to use #image to display solid-color backgrounds, but I don’t want to use solid-color images directly.
In most cases where you’d use an image you can also use a Rectangle Function – Typst Documentation instead:
I can’t really think of any situation where you must use an image. (Note that if if you use a rect in a figure, it will also be considered to have kind: image and be numbered along the other image-containing figures)
In case you really are restricted to only using images, here is a function that creates a 1x1 pixel GIF of the color you specify. Then that can be scaled to the size you want (note effect of fit):
#let img-bytes(col) = {
//Get values for R, G, B (surely there's a cleaner way of getting these)
let r = int((rgb(col).components().at(0) / 100%) * 0xFF)
let g = int((rgb(col).components().at(1) / 100%) * 0xFF)
let b = int((rgb(col).components().at(2) / 100%) * 0xFF)
//Create the GIF byte array
let img = (0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x01, 0x00, r, g, b, 0xFF, 0xFF, 0xFF, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3B)
//Return the "image" as bytes type
return bytes(img)
}
#image(img-bytes(red), format: "gif", width: 3cm, fit: "cover")
6 Likes
