Skip to content

Commit

Permalink
Fix area Region color
Browse files Browse the repository at this point in the history
  • Loading branch information
lcallarec committed Jan 14, 2024
1 parent 7e19760 commit 7ead357
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 90 deletions.
12 changes: 10 additions & 2 deletions src/region.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace LiveChart {
public delegate Coord? GetIntersection(double at_value);

public class Region {
public Gdk.RGBA line_color { get; set; default = Gdk.RGBA () { red = 1f, green = 1f, blue = 1f, alpha = 1f }; }
public Gdk.RGBA area_color { get; set; default = Gdk.RGBA () { red = 1f, green = 1f, blue = 1f, alpha = 0.5f }; }
public Gdk.RGBA? line_color { get; set; }
public Gdk.RGBA? area_color { get; set; }

private double floor;
private double ceil;
Expand All @@ -27,6 +27,14 @@ namespace LiveChart {
this.resolver = new CrossRegionResolver(this.floor, this.ceil);
}

public bool has_line_color() {
return this.line_color != null;
}

public bool has_area_color() {
return this.area_color != null;
}

public Region.between(double above, double below) {
this(above, below);
}
Expand Down
29 changes: 13 additions & 16 deletions src/smooth_line.vala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Cairo;

namespace LiveChart {

public class SmoothLine : SerieRenderer {

public Region? region {get; set; default = null; }
private SmoothLineDrawer drawer = new SmoothLineDrawer();
private SmoothLineLineDrawer line_drawer = new SmoothLineLineDrawer();
private SmoothLineSerieDrawer drawer = new SmoothLineSerieDrawer();

public SmoothLine(Values values = new Values()) {
base();
Expand All @@ -17,24 +17,19 @@ namespace LiveChart {
drawer.draw(ctx, config, this.line, Points.create(values, config), this.region);
}
}

public Cairo.Path draw_smooth_line(Points points, Context ctx, Config config) {
line_drawer.draw(ctx, config, this.line, points, region);
return ctx.copy_path();
}
}

public class SmoothLineDrawer {
private SmoothLineLineDrawer line_drawer = new SmoothLineLineDrawer();
private SmoothLineRegionOnLineDrawer region_on_line_drawer = new SmoothLineRegionOnLineDrawer();
public class SmoothLineSerieDrawer : Drawer {
private SmoothLineDrawer line_drawer = new SmoothLineDrawer();
private RegionOnLineDrawer region_on_line_drawer = new RegionOnLineDrawer();

public void draw(Context ctx, Config config, Path line, Points points, Region? region) {
public Intersections draw(Context ctx, Config config, Path line, Points points, Region? region) {
if(points.size > 0) {
var intersections = line_drawer.draw(ctx, config, line, points, region);
if (region != null) {
ctx.push_group();
}

var intersections = line_drawer.draw(ctx, config, line, points, region);
ctx.stroke();

if (region != null) {
Expand All @@ -44,11 +39,14 @@ namespace LiveChart {
ctx.pop_group_to_source();
ctx.paint();
}
}
return intersections;
}

return new Intersections();
}
}

public class SmoothLineLineDrawer : Drawer {
public class SmoothLineDrawer : Drawer {
public Intersections draw(Context ctx, Config config, Path line, Points points, Region? region) {
var intersections = new Intersections();
var first_point = points.first();
Expand Down Expand Up @@ -85,7 +83,7 @@ namespace LiveChart {
}
}

public class SmoothLineRegionOnLineDrawer {
public class RegionOnLineDrawer {
public void draw(Context ctx, Config config, Intersections intersections) {
var boundaries = config.boundaries();
intersections.foreach((intersection) => {
Expand All @@ -95,7 +93,6 @@ namespace LiveChart {
}
return true;
});
ctx.fill();
}
}
}
101 changes: 66 additions & 35 deletions src/smooth_line_area.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace LiveChart {
public class SmoothLineArea : SmoothLine {

public double area_alpha {get; set; default = 0.1;}
private SmoothLineAreaDrawer drawer = new SmoothLineAreaDrawer();
private SmoothLineAreaSerieDrawer drawer = new SmoothLineAreaSerieDrawer();

public SmoothLineArea(Values values = new Values()) {
base(values);
Expand All @@ -16,52 +16,83 @@ namespace LiveChart {
}
}

public class SmoothLineRegionAreaDrawer {
public void draw(Context ctx, Config config, Intersections intersections) {
var boundaries = config.boundaries();
intersections.foreach((intersection) => {
if (intersection != null) {
ctx.rectangle(intersection.start_x, boundaries.y.min, intersection.end_x - intersection.start_x, boundaries.height);
ctx.set_source_rgba(intersection.region.area_color.red, intersection.region.area_color.green, intersection.region.area_color.blue, intersection.region.area_color.alpha);
ctx.fill();
}
return true;
});
}
}

public class SmoothLineAreaDrawer {
public class SmoothLineAreaSerieDrawer {

private SmoothLineRegionAreaDrawer region_drawer = new SmoothLineRegionAreaDrawer();
private SmoothLineRegionOnLineDrawer region_on_line_drawer = new SmoothLineRegionOnLineDrawer();
private SmoothLineLineDrawer smooth_line_drawer = new SmoothLineLineDrawer();
private RegionAreaDrawer region_drawer = new RegionAreaDrawer();
private RegionOnLineDrawer region_on_line_drawer = new RegionOnLineDrawer();
private SmoothLineDrawer smooth_line_drawer = new SmoothLineDrawer();

public void draw(Context ctx, Config config, Path line, Points points, Gdk.RGBA color, double alpha, Region? region) {
if(points.size > 0) {
ctx.push_group();
var intersections = smooth_line_drawer.draw(ctx, config, line, points, region);
var path = ctx.copy_path();
var intersections = smooth_line_drawer.draw(ctx, config, line, points, region);
var curve = ctx.copy_path();

if(region != null && region.has_line_color()) {
ctx.push_group();
ctx.stroke();

ctx.stroke_preserve();
ctx.set_operator(Operator.IN);
region_on_line_drawer.draw(ctx, config, intersections);
ctx.fill();
ctx.pop_group_to_source();
ctx.paint();
} else {
ctx.stroke();
}

ctx.push_group();
ctx.append_path(curve);
var area = new Area(points, color, alpha);
area.draw(ctx, config);
ctx.fill();
ctx.set_operator(Operator.ATOP);
region_on_line_drawer.draw(ctx, config, intersections);
ctx.fill();
ctx.pop_group_to_source();

// Clear pixels that overlaps existing path
ctx.append_path(curve);
ctx.set_operator(Operator.CLEAR);
ctx.stroke();

ctx.save();
ctx.append_path(path);
area.draw(ctx, config);
ctx.clip();

region_drawer.draw(ctx, config, intersections);
ctx.restore();
if (region != null && region.has_area_color()) {

// Clear existing area if regions
ctx.set_operator(Operator.CLEAR);
region_drawer.draw(ctx, config, intersections);
}

ctx.pop_group_to_source();
ctx.paint();

if (region != null && region.has_area_color()) {
ctx.push_group();
//Create a clip to match the region area only, without the line
ctx.append_path(curve);
area.draw(ctx, config);
ctx.clip();

region_drawer.draw(ctx, config, intersections);

ctx.append_path(curve);
ctx.set_operator(Operator.CLEAR);
ctx.stroke();

ctx.pop_group_to_source();

ctx.paint();
}
}
}
}

public class RegionAreaDrawer {
public void draw(Context ctx, Config config, Intersections intersections) {
var boundaries = config.boundaries();
intersections.foreach((intersection) => {
if (intersection != null) {
ctx.set_source_rgba(intersection.region.area_color.red, intersection.region.area_color.green, intersection.region.area_color.blue, intersection.region.area_color.alpha);
ctx.rectangle(intersection.start_x, boundaries.y.min, intersection.end_x - intersection.start_x, boundaries.height);
}
return true;
});
ctx.fill();
}
}
}
9 changes: 5 additions & 4 deletions tests/fakes.vala
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ private ColorAtCoodinates get_color_at_from_pixels(uint8[] pixels, int stride, i
};
}

public void screenshot(TestContext context, string filename) {
public void screenshot(TestContext context, string suffix = "") {
int width = context.surface.get_width();
int height = context.surface.get_height();

var pixbuff = Gdk.pixbuf_get_from_surface(context.surface, 0, 0, width, height);
var test_path = Test.get_path().replace("/", "____");
try {
pixbuff.save(filename, "png");
} catch {

pixbuff.save(@"screenshots/$(test_path).png", "png");
} catch (Error e) {
message(@"Screenshot error in $(test_path): $(e.message)");
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/runner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ void main (string[] args) {
Test.init(ref args);
Gtk.init();

prepare_screenshots_directory();

register_config();
register_bounds();
register_values();
Expand All @@ -28,4 +30,21 @@ void main (string[] args) {
register_intersections();

Test.run();
}

private void prepare_screenshots_directory() {
try {
var directory = File.new_for_path("screenshots");
if(directory.query_exists()) {
Dir screenshot_dir = Dir.open("screenshots", 0);
string? name = null;
while ((name = screenshot_dir.read_name ()) != null) {
File.new_for_path(@"screenshots/$(name)").delete();
}
directory.delete();
}
directory.make_directory();
} catch (Error e) {
message(@"Error while preparing screenshots directory : $(e.message)");
}
}
Loading

0 comments on commit 7ead357

Please sign in to comment.