Skip to content

Commit

Permalink
Color boxes can use real alpha channels
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Nov 19, 2015
1 parent f36d01b commit a74de34
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
3 changes: 2 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ mdpopups.color_box
| border_size | int | No | 1 | Width of the color box border. If using `border2`, the value should be set to at least 2 to see both colors. |
| check_size | int | No | 4 | Size of checkered box squares used for the background of transparent colors. |
| max_colors | int | No | 5 | Max number of colors that will be evaluated in the `colors` parameter. Multiple colors are used to to create palette boxes showing multiple colors lined up horizontally. |
| alpha | bool | No | False | Will create color box images with a real alpha channel instead of simulating one with a checkered background. |

### syntax_highlight
mdpopups.syntax_highlight
Expand Down Expand Up @@ -351,7 +352,7 @@ css
h1, h2, h3, h4, h5, h6 { color: #888888 }
```

Some scopes might not be have colors assigned to them in a them, so multiple scopes can be defined, and the first one that matches will be used:
Some scopes might not have colors assigned to them, so multiple scopes can be defined, and the first one that matches will be used:

```css+jinja
/* If `keyword.operator` is not explicitly used, fallback to `.keyword` */
Expand Down
10 changes: 8 additions & 2 deletions st3/mdpopups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,16 @@ def md2html(view, markup):
).convert(markup).replace('"', '"').replace('\n', '')


def color_box(colors, border, border2=None, height=32, width=32, border_size=1, check_size=4, max_colors=5):
def color_box(
colors, border="#000000ff", border2=None, height=32, width=32,
border_size=1, check_size=4, max_colors=5, alpha=False
):
"""Color box."""

return colorbox.color_box(colors, border, border2, height, width, border_size, check_size, max_colors)
return colorbox.color_box(
colors, border, border2, height, width,
border_size, check_size, max_colors, alpha
)


def syntax_highlight(view, src, language=None, inline=False):
Expand Down
61 changes: 43 additions & 18 deletions st3/mdpopups/colorbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,40 @@
__all__ = ('color_box',)


def to_list(rgb):
def to_list(rgb, alpha=False):
"""
Break rgb channel itno a list.
Take a color of the format #RRGGBBAA (alpha optional and will be stripped)
and convert to a list with format [r, g, b].
"""
return [
int(rgb[1:3], 16),
int(rgb[3:5], 16),
int(rgb[5:7], 16)
]
if alpha:
return [
int(rgb[1:3], 16),
int(rgb[3:5], 16),
int(rgb[5:7], 16),
int(rgb[7:9], 16) if len(rgb) > 7 else 255
]
else:
return [
int(rgb[1:3], 16),
int(rgb[3:5], 16),
int(rgb[5:7], 16)
]


def checkered_color(color, background):
"""Mix color with the checkered color."""

checkered = RGBA(color)
checkered.apply_alpha(background)
return checkered.get_rgb()


def color_box(colors, border, border2=None, height=32, width=32, border_size=1, check_size=4, max_colors=5):
def color_box(
colors, border="#000000", border2=None, height=32, width=32,
border_size=1, check_size=4, max_colors=5, alpha=False
):
"""
Generate palette preview.
Expand All @@ -64,9 +76,9 @@ def color_box(colors, border, border2=None, height=32, width=32, border_size=1,
preview_colors = []
count = max_colors if len(colors) >= max_colors else len(colors)

border = to_list(border)
border = to_list(border, False)
if border2 is not None:
border2 = to_list(border2)
border2 = to_list(border2, False)

border1_size = border2_size = int(border_size / 2)
border1_size += border_size % 2
Expand All @@ -76,16 +88,29 @@ def color_box(colors, border, border2=None, height=32, width=32, border_size=1,

if count:
for c in range(0, count):
preview_colors.append(
(
to_list(checkered_color(colors[c], CHECK_LIGHT)),
to_list(checkered_color(colors[c], CHECK_DARK))
if alpha:
preview_colors.append(
(
to_list(colors[c], True),
to_list(colors[c], True)
)
)
else:
preview_colors.append(
(
to_list(checkered_color(colors[c], CHECK_LIGHT)),
to_list(checkered_color(colors[c], CHECK_DARK))
)
)
)
else:
preview_colors.append(
(to_list(CHECK_LIGHT), to_list(CHECK_DARK))
)
if alpha:
preview_colors.append(
(to_list('#00000000'), to_list('#00000000'))
)
else:
preview_colors.append(
(to_list(CHECK_LIGHT), to_list(CHECK_DARK))
)

color_height = height - (border_size * 2)
color_width = width - (border_size * 2)
Expand Down Expand Up @@ -155,7 +180,7 @@ def color_box(colors, border, border2=None, height=32, width=32, border_size=1,
f = io.BytesIO()

# Write out png
img = Writer(width, height)
img = Writer(width, height, alpha=alpha)
img.write(f, p)

# Read out png bytes and base64 encode
Expand Down

0 comments on commit a74de34

Please sign in to comment.