Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear and Radial Gradients on main branch #718

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
aab5b74
Create Pattern component to represent different fill types
AshishS-1123 Apr 18, 2022
d944b2a
Refactor existing code to use Pattern instead of component
AshishS-1123 Apr 18, 2022
0300364
Basic widgets for Editing gradients
AshishS-1123 Apr 18, 2022
11efd54
Use Cairo.Pattern for painting
AshishS-1123 Apr 19, 2022
1147247
Create Utility methods for handling patterns:
AshishS-1123 Apr 19, 2022
765b91a
Implement gradient editor. Modify pattern when mode is changed
AshishS-1123 Apr 20, 2022
bb29431
Refactor PatternTypeChooser
AshishS-1123 Apr 21, 2022
e6fef5e
Paint patterns on Canvas
AshishS-1123 Apr 21, 2022
56e653a
Draw nobs on canvas for gradients
AshishS-1123 Apr 23, 2022
61ef623
Fix position of gradients on canvas
AshishS-1123 Apr 23, 2022
938d48f
Move gradients and nobs and update corressponsing pattern
AshishS-1123 Apr 24, 2022
887a59b
Use selected pattern to draw the color button
AshishS-1123 Apr 25, 2022
7c84698
Use gradient editor to move stop colors on pattern component
AshishS-1123 Apr 25, 2022
9545486
Modify gradient stop color using color chooser
AshishS-1123 Apr 25, 2022
96f7f1e
Merge branch 'main' of github.com:AshishS-1123/Akira into gradient2_t…
AshishS-1123 Apr 25, 2022
705c24e
Use gradients for border of canvas item:
AshishS-1123 Apr 27, 2022
658c1ea
Prevent fill pattern from changing when border pattern modified.
AshishS-1123 Apr 27, 2022
5d52c22
Implement radial gradient
AshishS-1123 Apr 27, 2022
d42cb08
Save and load patterns from global colors
AshishS-1123 Apr 30, 2022
c55876a
Prvent weird color popover bug
AshishS-1123 Apr 30, 2022
df0fae0
Fix minor bug in gradient nobs
AshishS-1123 May 1, 2022
168a716
Fix some more silly mistakes
AshishS-1123 May 1, 2022
8a26b25
Merge branch 'main' of github.com:AshishS-1123/Akira into gradient2_t…
AshishS-1123 Aug 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Drawables/Drawable.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class Akira.Drawables.Drawable {

// Style
public double line_width { get; set; default = 0; }
public Gdk.RGBA fill_rgba { get; set; default = Gdk.RGBA (); }
public Gdk.RGBA stroke_rgba { get; set; default = Gdk.RGBA (); }
public Cairo.Pattern fill_pattern { get; set; default = new Cairo.Pattern.rgba (1, 1, 1, 1); }
public Cairo.Pattern border_pattern { get; set; default = new Cairo.Pattern.rgba (1, 1, 1, 1); }
public BorderType border_type { get; set; default = BorderType.CENTER; }
public double radius_tr { get; set; default = 0; }
public double radius_tl { get; set; default = 0; }
Expand Down Expand Up @@ -383,7 +383,7 @@ public class Akira.Drawables.Drawable {
if (draw_type == DrawType.XRAY) {
return false;
}
context.set_source_rgba (fill_rgba.red, fill_rgba.green, fill_rgba.blue, fill_rgba.alpha);
context.set_source (fill_pattern);
context.set_antialias (Cairo.Antialias.GRAY);
return true;
}
Expand All @@ -396,7 +396,7 @@ public class Akira.Drawables.Drawable {
return true;
}

context.set_source_rgba (stroke_rgba.red, stroke_rgba.green, stroke_rgba.blue, stroke_rgba.alpha);
context.set_source (border_pattern);
context.set_line_width (line_width);
context.set_antialias (Cairo.Antialias.GRAY);
return line_width > 0;
Expand Down
14 changes: 10 additions & 4 deletions src/Layouts/BordersList/BorderItemModel.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class Akira.Layouts.BordersList.BorderItemModel : Models.ColorModel {
private unowned Lib.ViewCanvas _view_canvas;

private Lib.Items.ModelInstance _cached_instance;
// private Lib.Items.ModelInstance _cached_instance;

public int border_id;

Expand All @@ -39,9 +39,10 @@ public class Akira.Layouts.BordersList.BorderItemModel : Models.ColorModel {
var node = im.item_model.node_from_id (_cached_instance.id);
assert (node != null);

var new_color = Lib.Components.Color.from_rgba (color, hidden);
var new_pattern = pattern.copy ();
var new_borders = node.instance.components.borders.copy ();
new_borders.replace (Lib.Components.Borders.Border (border_id, new_color));
var new_border = new_borders.border_from_id (border_id).with_replaced_pattern (new_pattern);
new_borders.replace (new_border);
node.instance.components.borders = new_borders;

im.item_model.alert_node_changed (node, Lib.Components.Component.Type.COMPILED_BORDER);
Expand Down Expand Up @@ -74,7 +75,12 @@ public class Akira.Layouts.BordersList.BorderItemModel : Models.ColorModel {
(blocker);

var border = _cached_instance.components.borders.border_from_id (border_id);
color = border.color;
active_pattern_type = border.active_pattern;

solid_pattern = border.solid_pattern;
linear_pattern = border.linear_pattern;
radial_pattern = border.radial_pattern;

hidden = border.hidden;
}
}
2 changes: 1 addition & 1 deletion src/Layouts/BordersList/BorderListItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Akira.Layouts.BordersList.BorderListItem : VirtualizingListBoxRow {
context.add_class ("selected-color-container");
context.add_class ("bg-pattern");

color_button = new Widgets.ColorButton ();
color_button = new Widgets.ColorButton (view_canvas.window);
container.add (color_button);

eyedropper_button = new Widgets.EyeDropperButton () {};
Expand Down
14 changes: 10 additions & 4 deletions src/Layouts/FillsList/FillItemModel.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class Akira.Layouts.FillsList.FillItemModel : Models.ColorModel {
private unowned Akira.Lib.ViewCanvas _view_canvas;

private Lib.Items.ModelInstance _cached_instance;
// private Lib.Items.ModelInstance _cached_instance;

public int fill_id;

Expand All @@ -39,9 +39,10 @@ public class Akira.Layouts.FillsList.FillItemModel : Models.ColorModel {
var node = im.item_model.node_from_id (_cached_instance.id);
assert (node != null);

var new_color = Lib.Components.Color.from_rgba (color, hidden);
var new_pattern = pattern.copy ();
var new_fills = node.instance.components.fills.copy ();
new_fills.replace (Lib.Components.Fills.Fill (fill_id, new_color));
var new_fill = new_fills.fill_from_id (fill_id).with_replaced_pattern (new_pattern);
new_fills.replace (new_fill);
node.instance.components.fills = new_fills;

im.item_model.alert_node_changed (node, Lib.Components.Component.Type.COMPILED_FILL);
Expand Down Expand Up @@ -74,7 +75,12 @@ public class Akira.Layouts.FillsList.FillItemModel : Models.ColorModel {
(blocker);

var fill = _cached_instance.components.fills.fill_from_id (fill_id);
color = fill.color;
active_pattern_type = fill.active_pattern;

solid_pattern = fill.solid_pattern;
linear_pattern = fill.linear_pattern;
radial_pattern = fill.radial_pattern;

hidden = fill.hidden;
}
}
2 changes: 1 addition & 1 deletion src/Layouts/FillsList/FillListItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Akira.Layouts.FillsList.FillListItem : VirtualizingListBoxRow {
context.add_class ("selected-color-container");
context.add_class ("bg-pattern");

color_button = new Widgets.ColorButton ();
color_button = new Widgets.ColorButton (view_canvas.window);
container.add (color_button);

eyedropper_button = new Widgets.EyeDropperButton () {};
Expand Down
92 changes: 79 additions & 13 deletions src/Lib/Components/Borders.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,79 @@
public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {
public struct Border {
public int _id;
public Color _color;
public Pattern _pattern {
get {
switch (active_pattern) {
case Pattern.PatternType.SOLID:
return solid_pattern;
case Pattern.PatternType.LINEAR:
return linear_pattern;
case Pattern.PatternType.RADIAL:
return radial_pattern;
default:
return solid_pattern;
}
}

set {
switch (active_pattern) {
case Pattern.PatternType.SOLID:
solid_pattern = value;
break;
case Pattern.PatternType.LINEAR:
linear_pattern = value;
break;
case Pattern.PatternType.RADIAL:
radial_pattern = value;
break;
default:
solid_pattern = value;
break;
}
}
}

// Each border item will have patterns for all three types,
// so that user can easily switch between them.
// However, the non active patterns will not be serialized.
public Pattern solid_pattern;
public Pattern linear_pattern;
public Pattern radial_pattern;

public Pattern.PatternType active_pattern;

public double _size;

public Border (int id = -1, Color color = Color (), double? size = null) {
public Border (int id = -1, Pattern pattern = new Pattern (), double? size = null) {
_id = id;
_color = color;
active_pattern = Pattern.PatternType.SOLID;

var border_rgba = Gdk.RGBA ();
border_rgba.parse (settings.border_color);
solid_pattern = new Pattern.solid (border_rgba, false);

linear_pattern = new Pattern.linear (Geometry.Point (5, 5), Geometry.Point (95, 95), false);
radial_pattern = new Pattern.radial (Geometry.Point (5, 5), Geometry.Point (95, 95), false);

_size = size != null ? size : settings.border_size;
}

public Border.deserialized (int id, Json.Object obj) {
_id = id;
_color = Color.deserialized (obj.get_object_member ("color"));
_size = (double)obj.get_int_member ("size");
}

public Border.with_all_patterns (int id, Pattern solid_pattern, Pattern linear_pattern, Pattern radial_pattern, Pattern.PatternType type) {
this.solid_pattern = solid_pattern;
this.linear_pattern = linear_pattern;
this.radial_pattern = radial_pattern;
this.active_pattern = type;
}

public Json.Node serialize () {
var obj = new Json.Object ();
obj.set_int_member ("id", _id);
obj.set_member ("color", _color.serialize ());
obj.set_member ("pattern", _pattern.serialize ());
obj.set_double_member ("size", _size);
var node = new Json.Node (Json.NodeType.OBJECT);
node.set_object (obj);
Expand All @@ -53,14 +107,14 @@ public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {
return _id;
}
}
public Gdk.RGBA color {
public Pattern pattern {
get {
return _color.rgba;
return _pattern;
}
}
public bool hidden {
get {
return _color.hidden;
return _pattern.hidden;
}
}
public double size {
Expand All @@ -71,11 +125,20 @@ public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {

// Mutators.
public Border with_color (Color new_color) {
return Border (_id, new_color, _size);
Pattern pattern = new Pattern.solid (new_color.rgba, new_color.hidden);
var border = Border (id, pattern, _size);
return border;
}

public Border with_replaced_pattern (Pattern new_pattern) {
var new_border = Border.with_all_patterns (_id, solid_pattern, linear_pattern, radial_pattern, new_pattern.type);
new_border._pattern = new_pattern;

return new_border;
}

public Border with_size (double new_size) {
return Border (_id, _color, new_size);
return Border (_id, _pattern, new_size);
}
}

Expand All @@ -87,7 +150,7 @@ public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {

public Borders.single_color (Color color, int size) {
data = new Border[1];
data[0] = Border (0, color, size);
data[0] = Border (0, new Pattern.solid (color.rgba, color.hidden), size);
}

public Borders.deserialized (Json.Object obj) {
Expand Down Expand Up @@ -124,7 +187,7 @@ public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {
public Border? border_from_id (int id) {
foreach (unowned var border in data) {
if (border.id == id) {
return border.with_color (border._color);
return Border.with_all_patterns (id, border.solid_pattern, border.linear_pattern, border.radial_pattern, border.active_pattern);
}
}
return null;
Expand All @@ -150,7 +213,10 @@ public class Akira.Lib.Components.Borders : Component, Copyable<Borders> {
latest_id = int.max (latest_id, border.id);
}
latest_id++;
var border = Border ((int) latest_id, color, size);

var pattern = new Pattern.solid (color.rgba, color.hidden);
var border = Border ((int) latest_id, pattern);

data.resize (data.length + 1);
data[data.length - 1] = border;
}
Expand Down
44 changes: 24 additions & 20 deletions src/Lib/Components/CompiledBorder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
*/

public class Akira.Lib.Components.CompiledBorder : Copyable<CompiledBorder> {
private Gdk.RGBA _color;
private Pattern _pattern;
private double _size;
private bool _visible;

public Gdk.RGBA color {
get { return _color; }
public Pattern pattern {
get { return _pattern; }
}

public double size {
Expand All @@ -36,60 +36,64 @@ public class Akira.Lib.Components.CompiledBorder : Copyable<CompiledBorder> {
get { return _visible; }
}

public CompiledBorder (Gdk.RGBA color, double size, bool visible) {
_color = color;
public CompiledBorder (Pattern pattern, double size, bool visible) {
_pattern = pattern;
_size = size;
_visible = visible;
}

public CompiledBorder.as_empty () {
_color = Gdk.RGBA () { red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0};
_pattern = new Pattern.solid (Gdk.RGBA () {red = 0, green = 0, blue = 0, alpha = 0}, false);
_size = 0;
_visible = false;
}

public CompiledBorder copy () {
return new CompiledBorder (_color, _size, _visible);
return new CompiledBorder (_pattern, size, _visible);
}

public static CompiledBorder compile (Components? components, Lib.Items.ModelNode? node) {
var rgba_border = Gdk.RGBA ();
var pattern_border = new Pattern ();
bool has_colors = false;
double size = 0;
double border_size = 0;

if (components == null) {
return new CompiledBorder (rgba_border, size, has_colors);
return new CompiledBorder (pattern_border, border_size, has_colors);
}

unowned var borders = components.borders;
unowned var opacity = components.opacity;

// Set an initial arbitrary color with full transparency.
rgba_border.alpha = 0;
unowned var size = components.size;
unowned var center = components.center;

if (borders == null) {
return new CompiledBorder (rgba_border, size, false);
return new CompiledBorder (pattern_border, border_size, has_colors);
}

// Loop through all the configured borders and reload the color.
// Loop through all the configured borders.
for (var i = 0; i < borders.data.length; ++i) {
// Skip if the border is hidden as we don't need to blend colors.
if (borders.data[i].hidden) {
continue;
}

// Set the new blended color.
rgba_border = Utils.Color.blend_colors (rgba_border, borders.data[i].color);
size = double.max (size, borders.data[i].size);
// rgba_border = Utils.Color.blend_colors (rgba_border, borders.data[i].pattern.get_first_color ());
border_size = borders.data[i].size;
pattern_border = Utils.Pattern.create_pattern_with_converted_positions (borders.data[i].pattern, size, center);
has_colors = true;

// TODO: Temporarily disable blending patterns. Not implemented.
break;
}

// Apply the mixed RGBA value only if we had one.
if (has_colors && opacity != null) {
// Keep in consideration the global opacity to properly update the border color.
rgba_border.alpha = rgba_border.alpha * opacity.opacity / 100;
// Keep in consideration the global opacity to properly update the fill color.
// TODO: Disable this too.
// rgba_border.alpha = rgba_border.alpha * opacity.opacity / 100;
}

return new CompiledBorder (rgba_border, size, has_colors && size != 0);
return new CompiledBorder (pattern_border, border_size, has_colors);
}
}
Loading