Skip to content

๐Ÿง  How to Read Gradient Pattern DSL โ€‹

Gradient Pattern DSL may look complex at first, but it follows a small set of consistent rules. This section teaches you how to read any pattern step by step.

๐Ÿ“Œ Start from the boundaries โ€‹

Every pattern always has:

txt
^ ... .

Read it as:

txt
Start of pattern
...
End of pattern

๐Ÿง  Example: Basic repeating pattern โ€‹

Pattern โ€‹

txt
^[color-stop,~color-stop].

๐Ÿ“Œ Step 1 - boundaries โ€‹

txt
^ - Begin pattern  
. - End of pattern

So we know:

everything between ^ and . is the rule we need to read

๐Ÿ“Œ Step 2 - look inside โ€‹

txt
[color-stop,~color-stop]

This is a sequence ([]), which means:

txt
do this, then this

๐Ÿ“Œ Step 3 - first element โ€‹

txt
color-stop

This means:

the pattern must start with a color-stop

๐Ÿ“Œ Step 4 - second element โ€‹

txt
~color-stop

The ~ operator means:

txt
repeat zero or more times

So:

txt
~color-stop

means:

zero or more additional color-stops

๐Ÿ“Œ Step 5 - combine everything โ€‹

Now combine both parts:

txt
[color-stop,~color-stop]

Becomes:

txt
one color-stop
then any number of additional color-stops

๐Ÿ“Œ Final explanation โ€‹

txt
Start of pattern

Expect:
  one color-stop

Then:
  zero or more additional color-stops

End of pattern

๐Ÿ’ก Intuition โ€‹

You can read this pattern as:

โ€œat least one color-stop, then as many as you wantโ€

๐Ÿ”— Try it โ€‹

Open the playground and test:

txt
color-stop
color-stop, color-stop
color-stop, color-stop, color-stop

All of them should be valid.

๐Ÿ“Œ Understand sequence [] โ€‹

txt
[a,b]

Means:

txt
a then b

So:

txt
[color-stop,~color-stop]

Becomes:

txt
color-stop, then repeat color-stop N-times

๐Ÿ“Œ Understand repetition ~ โ€‹

txt
~color-stop

Means:

txt
zero or more color-stop

So the full meaning:

txt
at least one color-stop
then any number of additional color-stops

๐Ÿ“Œ Final result โ€‹

txt
^[color-stop,~color-stop].

`^` - Start of pattern

Expect:
  at least one `color-stop`

Then:
  zero or more additional color-stops

`.` - End of pattern

๐Ÿ“Œ Reading OR conditions โ€‹

Now a more complex pattern:

txt
(config|color-stop)

This means:

txt
either config
or color-stop

๐Ÿ“Œ Combining sequence and OR โ€‹

txt
([config,color-stop]|color-stop)

Read it as:

txt
Either:
  config followed by color-stop
Or:
  color-stop

๐Ÿ“Œ Reading nested patterns โ€‹

Example:

txt
^[([config,color-stop]|color-stop),~color-stop].

Step-by-step:

txt
Start of pattern

Then:
  Either:
    config followed by color-stop
  Or:
    color-stop

Then:
  repeat color-stop

End of pattern

๐Ÿ“Œ Color hints โ€‹

txt
[color-hint,color-stop]

Means:

txt
a color-hint must always be followed by a color-stop

๐Ÿง  Mental model โ€‹

Think of DSL as a set of instructions:

txt
Read input from left to right
Match expected tokens
Branch when needed
Repeat when allowed

๐Ÿ”— Try it interactively โ€‹

Reading patterns becomes much easier when you see them visually.

๐Ÿ‘‰ Open the playground to explore patterns interactively:

Open DSL Playground โ†’

๐Ÿš€ Summary โ€‹

  • ^ โ†’ BEGIN
  • . โ†’ END
  • [] โ†’ SEQUENCE
  • () โ†’ GROUPPING
  • | โ†’ OR
  • ~ โ†’ REPETITION (Array)
  • config โ†’ CONFIG (angle, dimension, position)
  • color-stop โ†’ COLOR STOP (red, gren 10% 20%, blue 50%, rgb(1,2,3))
  • color-hint โ†’ COLOR HINT (10%, 20%, 50%) - offset of the gradient intersection point

Once you understand these, you can read any pattern.

๐Ÿ’ก Tip โ€‹

Start simple:

txt
^color-stop.

Then gradually add:

txt
[]
()
|
~