What's the best way to create a linear gradiant in libvips? #4396
Replies: 1 comment 1 reply
-
Hello @philocalyst, There's no operation for creating a gradient, but you can make one yourself very easily. For example, in ruby (it'd be almost the same in python / php / C++, etc.) you could write: def gradient(start, stop)
lut = Vips::Image.identity / 255
lut = lut * start + (lut * -1 + 1) * stop
lut.colourspace(:srgb, :source_space => :lab)
end That makes an sRGB 8-bit lookup table which fades between two CIELAB colours. You then write an index function which generates 0-255 in some way, perhaps: width = 500
height = 500
p1 = [100, 100]
p2 = [400, 400]
index = Vips::Image.sdf(width, height, "line", a: p1, b:p2) That's a 500 by 500 pixel image with a line of 0s connecting the two points, and all other points falling away by distance. Then you can apply the gradient with: red = [50, 100, 0]
green = [50, -100, 0]
gradient = index.maplut(gradient(red, green)) To make: (less attractive than I'd hoped) The function list is handy for looking up these operations (identity, colourspace, sdf, maplut): https://www.libvips.org/API/current/func-list.html Full program: #!/usr/bin/ruby
require 'vips'
def gradient(start, stop)
lut = Vips::Image.identity / 255
lut = lut * start + (lut * -1 + 1) * stop
lut.colourspace(:srgb, :source_space => :lab)
end
width = 500
height = 500
p1 = [100, 100]
p2 = [400, 400]
index = Vips::Image.sdf(width, height, "line", a: p1, b: p2)
red = [50, 100, 0]
green = [50, -100, 0]
gradient = index.maplut(gradient(red, green))
gradient.write_to_file("result.png") |
Beta Was this translation helpful? Give feedback.
-
Even just a simple transition between two colors?
Beta Was this translation helpful? Give feedback.
All reactions