Using Lua to ScITE PDF Export with TextMate Color Change

Jan
10
2007

I've been using the ScITE editor and the customizations to it for a couple of weeks now (yes, I'm also messing with jEdit so no hate mail). I'm using both the Textmate-snippet-extras bundle and the Exporters bundle. This applies to the combination of both.

I love the color scheme, except when I use the exports. Since the Textmate color scheme is light gray on dark gray, the PDF exports (you've seen a few on the site in the last couple of weeks) have the main text elements like braces and non-highlighted text in a really light gray.

I hacked up an HTML exporter to fix this and meet my needs, complete with Creative Commons license notice, etc. as well as one for work projects, etc. However, the one I really want to have handy is the PDF export.

When I went into the PDFPlus exporter to customize it, I had to dig a bit and get my bearings. First, it's Lua, which I've never needed to use before. Second, it's PDF, which, compared to the HTML/XML I spend so much time in, is like reading reverse Polish notation.

I finally figured out where to intercept the color and make my change.

Here's what the PDFPlus exporter had in it:

prstyle.fore = colorElem(sd.fore.r)..
colorElem(sd.fore.g)..
colorElem(sd.fore.b)

Basically, sd.fore is the foreground color, accessible by 3 different properties: sd.fore.r, sd.fore.g, sd.fore.b for red, green blue. The value of those is 0 to 255. As with all 3 part RGB color codes, if they all equal each other, you're dealing with a shade of gray with 0 being black and 255 being white. The ".." between are how Lua concatenates strings together (instead of the "+" or "." you may be used to from other languages).

Since the Textmate-style theme I'm using for ScITE has the main color set to 170 on the 255 scale, it's really light when you remove the dark background. The other colors are OK, it's just the light grays.

So, the solution I implemented was to bump any light grays to darker grays. I first check to see if all 3 RGB codes match (to make sure it's gray) and then check the number against 125 (an arbitrary number about half gray). If that's the case, I drop it darker.

Here's what I changed it to:

if
(sd.fore.r == sd.fore.g) and
(sd.fore.g == sd.fore.b) and
(sd.fore.r > 125 )
then
prstyle.fore = "0.3 0.3 0.3 "
else
prstyle.fore = colorElem(sd.fore.r)..
colorElem(sd.fore.g)..
colorElem(sd.fore.b)
end

You'll note in the "then" that I actually set the prstyle.fore to a set of "0.3" entries instead of something on the 255 scale. One of the joys of working with these exporters is that each of the target formats uses colors in a different numeric system. Some are hex codes (where our 170 gray is #AAAAAA), some as 0-255 (where it's 170) and then the PDF one, which uses 0-1 (where 0.3 is 30% toward white from black).

In the end, pretty simple. Here's a PDF of the exporter in question, bootstrapped with itself.

 

J Wynia

For better or worse, I'm the guy who runs things here. I'm a web consultant, software developer, writer and geek from Minneapolis, MN. This site is a fairly wide cross-section of the things I'm interested in and enjoy writing about. If you'd like a more "real-time" slice of my thoughts, you should follow me on Twitter here.

Oh, and if you happen to be looking for hosting for your Subversion repositories or just web hosting in general, take a look at Dreamhost. It's what I use for Subversion and your signup helps me out.

Feeds and Links


www.flickr.com
This is a Flickr badge showing public photos from J Wynia. Make your own badge here.

Search


Pages

Archives

Computers Blog Directory
© 2003-2010 J Wynia. All original content is licensed under the terms of the Creative Commons Attribution license unless otherwise noted. Content from other sources is licensed under its original terms.