-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snippet of the gradient regex to specify
- Loading branch information
1 parent
5ecec19
commit 62a9959
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
def parse_color(str) | ||
stops = / # used to capture the stops | ||
\s* # leading whitespace is ok | ||
([\w]+) # color | ||
@ # no spaces here | ||
(\d+\.?\d*) # offset number | ||
/x | ||
linear_gradient = / | ||
\( # coordinate 1 | ||
(\d+\.?\d*) # x1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # y1 number | ||
\) | ||
\s* # space between coordinates is ok | ||
\( # coordinate 2 | ||
(\d+\.?\d*) # x2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # y2 number | ||
\) | ||
(#{stops})+ # stops | ||
\s* # trailing whitespace is ok | ||
/x | ||
radial_gradient = / | ||
\( # coordinate 1 | ||
(\d+\.?\d*) # x1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # y1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # r1 number | ||
\) | ||
\s* # space between coordinates is ok | ||
\( # coordinate 2 | ||
(\d+\.?\d*) # x2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # y2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) # r1 number | ||
\) | ||
(#{stops})+ # stops | ||
\s* # trailing whitespace is ok | ||
/x | ||
|
||
if match = str.match(linear_gradient) | ||
x1,y1,x2,y2 = match.captures | ||
stops = str.scan(stops) | ||
puts "Coords: x1: #{x1}, y1: #{y1}, x2: #{x2}, #{y2}" | ||
puts "Stops: #{stops}" | ||
elsif match = str.match(radial_gradient) | ||
x1,y1,r1,x2,y2,r2 = match.captures | ||
stops = str.scan(stops) | ||
puts "Coords: coord1: #{x1}, #{y1},#{r1} | coord2: #{x2}, #{y2},#{r2}" | ||
puts "Stops: #{stops}" | ||
else | ||
puts "No match" | ||
end | ||
end | ||
|
||
parse_color("(1.0, 2.0) (3.0, 4.0) blue@0.5 red@1.0") | ||
parse_color("(1.0,2.0)(3.0,4.0)green@0.5yellow@1.0") | ||
parse_color("(1.0, 2.0, 5.0)(3.0, 4.0, 6.0) green@0.5 yellow@1.0 ") |