BrushCue Example: Color Gradient¶

Open In Colab

You can use this tool online at https://www.brushcue.com/tools/color-gradient

In [ ]:
!pip install brushcue
In [1]:
import io
from PIL import Image

import brushcue as bc

width = 1024
width_f = bc.Int.to_float(width)
height = 1024
height_f = bc.Int.to_float(height)
angle_degrees = 0.0
start_color = bc.ProfiledColor.from_srgb_hex("#1F40D9")
end_color = bc.ProfiledColor.from_srgb_hex("#FF8C26")
graph = bc.Composition.freeform_shader(
    "let angle_rad = angle_degrees * 0.0174532925;\nlet dir = vec2<f32>(cos(angle_rad), sin(angle_rad));\nlet size = vec2<f32>(width_f, height_f);\nlet proj = dot(canvas_position, dir);\nlet c1 = dot(vec2<f32>(0.0, 0.0), dir);\nlet c2 = dot(vec2<f32>(size.x, 0.0), dir);\nlet c3 = dot(vec2<f32>(0.0, size.y), dir);\nlet c4 = dot(vec2<f32>(size.x, size.y), dir);\nlet lo = min(min(c1, c2), min(c3, c4));\nlet hi = max(max(c1, c2), max(c3, c4));\nlet t = clamp((proj - lo) / max(hi - lo, 0.00001), 0.0, 1.0);\nreturn mix(start_color, end_color, t);",
    "",
    bc.Bounds2f.from_x_y_width_height(0.0, 0.0, width_f, height_f),
    bc.ColorRepresentation.srgb(),
    bc.Dictionary.create()
    .add("angle_degrees", angle_degrees)
    .add("width_f", width_f)
    .add("height_f", height_f)
    .add("start_color", start_color)
    .add("end_color", end_color),
)

ctx = bc.Context()
composition = graph.execute(ctx)
data_bytes = composition.to_image_bytes(ctx)
img = Image.open(io.BytesIO(data_bytes))
img.thumbnail((400, 400))  # Remove this line for full resolution
img
[wgpu] using backend Metal — adapter 'Apple M5' (IntegratedGpu), driver ''
Out[1]:
No description has been provided for this image