Albin Larsson: Blog

Culture, Climate, and Code

Rounding Colors with Colorsnap

9th September 2018

While working on an evaluation of Generous Interfaces in the GLAM sector at the Swedish National Heritage Board we extracted colors in images to make them searchable by their palettes. For usability reasons we wanted to limit the extracted colors to a comprehensible palette.

Screenshot of our generous interface.

Colorsnap is a Python package for snapping/rounding colors to other colors/palettes. Although I could find plenty of resources and examples on how to do this, I could not find a accessible package.

So now you can do:

pip install colorsnap

Let’s take the hex color #0000ba and round it to its nearest color available as a named color in the CSS 3 specification.

from colorsnap.palettes import CSS_3
from colorsnap import snap_color

snap_color(CSS_3, '#0000ba')
# returns the following tuple ('#0000ba', 'mediumblue')

visualized (input to the left):

Colors compared.

Colorsnap comes with palettes for CSS 2, 2.1, 3, and 4 but you can also use your own:

from colorsnap import snap_color

palette = {
    'black': '#000000',
    'gray': '#808080',
    'white': '#ffffff',
}

snap_color(palette, '#0000ba')

In addition to using named colors you can also round colors to basic hex values:

from colorsnap import snap_color

palette = ['#4286f4', '#414449']

snap_color(palette, '#0000ba')

It’s available over at PyPI and Github.

A Web App for Browsing Sign Languages on Wikidata

14th August 2018

I recently built a web app for browsing and learning sign languages. The original goal was to build an interface on top of SparQL and showcase sign languages content on Wikimedia Commons. This weekend I finally took the time to deploy it. You can try it out here.

Screenshot of the application.

7 Online Web Development Tools I Use

10th August 2018

Seven of my favorite online tools for web development.

Screenshot of the Coolors tool.

Coolors

I do not think that the Coolors app helps me enough when it comes to selecting a palette for my next project, but hey, we can’t expect that. With its great user experience and useful sharing options it’s my go to tool for creating or sharing color palettes.

Screenshot of the RegExr tool.

RegExr

RegExr does not only have great UI for writing and testing Regular Expressions but it also provides a library of others solutions and a cheatsheet.

Real Favicon Generator

Safari, Chrome, Edge, they all got their own type of special icons and manifests these days. The Real Favicon Generator allows me to upload a SVG and specify some settings to generate these and icons for all common devices and environments.

JSON Editor Online

Whenever I need a non developer to edit a JSON document I send them to JSON Editor Online, It’s GUI allows for easy editing and sharing.

GeoJSON.io

It’s not uncommon that I need some static GeoJSON. GeoJSON.io allows me to draw geometries and add properties as I wish.

Keycode.info

Need a key code? Just head over to keycode.info and click the key you need the code for, especially useful when you work with custom keyboards such as remote controls.

gitignore.io

No need to patch your .gitignore files as you go. Make sure that you ignore what you need from the beginning. Nowadays I use this through an VS Code extension.

Cache Busting Wikidata SparQL Queries

26th January 2018

UPDATE: By setting a cache-control: no-cache header you can disable this query caching.

The problem

Whenever you write a Wikidata query form which you do not expect the result to be the same each time, you will run into the issue of caching. Lets take the following example, returning a random cat:

SELECT ?item ?itemLabel (MD5(CONCAT(STR(?item), STR(RAND()))) as ?random)
WHERE 
{
  ?item wdt:P31 wd:Q146.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], en". }
} ORDER BY ?random 
LIMIT 1

Run in the Wikidata Query Editor

If you run this once you will get a random cat if you run this twice you will get the same random cat.

This is because Wikidata has saved the result, to serve you faster the second time.

The Solution

A solution some people uses is to “just add a space somewhere”, this is possible because Wikidata cache queries based on the SparQL string, not the actual parsed and preformed query.

When used in implementations keeping track of spaces is not an option so I decided to use random comments that could be generated from hashing functions etc:

#01e8c03a6bdfe392431d8189130fcfc0
SELECT ?item ?itemLabel (MD5(CONCAT(STR(?item), STR(RAND()))) as ?random)
WHERE 
{
  ?item wdt:P31 wd:Q146.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], en". }
} ORDER BY ?random 
LIMIT 1

Changing the comment string and rerunning the query will result in an new random cat.

When actually used in real world implementations where you might minify your queries I suggest appending the comment just before preforming it.

Final Notes

Older PostsNewer Posts