Editorial Workflows

Insert Color

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Shows a custom UI with three sliders (RGB or HSV) to insert an HTML hex color. If an existing color is selected in the editor, the initial values of the sliders are adjusted accordingly.

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import workflow
import editor
import colorsys
import ui
import re

def parse_hex(s):
	if s.startswith('#'):
		s = s[1:]
	if len(s) == 3:
		s = ''.join(c*2 for c in s)
	if re.match('[0-9A-Fa-f]{6}', s):
		r = int(s[:2], 16) / 255.0
		g = int(s[2:4], 16) / 255.0
		b = int(s[4:], 16) / 255.0
		return (r, g, b)
	return None

def slider_action(sender):
	sv = sender.superview
	mode = sv['mode_control'].selected_index
	if mode == 1:
		h = sv['slider1'].value
		s = sv['slider2'].value
		b = sv['slider3'].value
		rgb = colorsys.hsv_to_rgb(h, s, b)
	else:
		r = sv['slider1'].value
		g = sv['slider2'].value
		b = sv['slider3'].value
		rgb = (r, g, b)
	sv['hex_label'].text = '%02x%02x%02x' % tuple(int(x*255) for x in rgb)
	sv['swatch'].background_color = rgb

def insert_action(sender):
	hex_color = '#' + v['hex_label'].text
	editor.insert_text(hex_color)
	cursor = editor.get_selection()[0]
	editor.set_selection(cursor-len(hex_color), cursor)
	v.close()

def mode_action(sender):
	mode = sender.selected_index
	sv = sender.superview
	if mode == 1:
		sv['label1'].text = 'Hue'
		sv['label2'].text = 'Saturation'
		sv['label3'].text = 'Brightness'
	else:
		sv['label1'].text = 'Red'
		sv['label2'].text = 'Green'
		sv['label3'].text = 'Blue'
	c1 = sv['slider1'].value
	c2 = sv['slider2'].value
	c3 = sv['slider3'].value
	f = colorsys.rgb_to_hsv if mode == 1 else colorsys.hsv_to_rgb
	color = f(c1, c2, c3)
	sv['slider1'].value = color[0]
	sv['slider2'].value = color[1]
	sv['slider3'].value = color[2]

class TextFieldDelegate (object):
	def textfield_did_change(self, tf):
		color = parse_hex(tf.text)
		if color:
			sv = tf.superview
			mode = sv['mode_control'].selected_index
			sv['swatch'].background_color = color
			if mode == 0:
				color = colorsys.rgb_to_hsv(*color)
			sv['slider1'].value = color[0]
			sv['slider2'].value = color[1]
			sv['slider3'].value = color[2]
			
v = ui.load_view()
v['mode_control'].action = mode_action
v['hex_label'].delegate = TextFieldDelegate()
v['hex_label'].autocapitalization_type = ui.AUTOCAPITALIZE_NONE
btn = ui.ButtonItem('Insert')
btn.action = insert_action
btn.tint_color = (0, 0.48, 1)
v.right_button_items = [btn]
sel_color = parse_hex(editor.get_selected_text())
if sel_color:
	v['slider1'].value = sel_color[0]
	v['slider2'].value = sel_color[1]
	v['slider3'].value = sel_color[2]
slider_action(v['slider1'])
v.present('popover', title_color=0.3, title_bar_color='white')