1 <?php
2 /**
3 * Contains Alfred class for Alphred, a class to work with some Alfred stuff
4 *
5 * PHP version 5
6 *
7 * @package Alphred
8 * @copyright Shawn Patrick Rice 2014
9 * @license http://opensource.org/licenses/MIT MIT
10 * @version 1.0.0
11 * @author Shawn Patrick Rice <rice@shawnrice.org>
12 * @link http://www.github.com/shawnrice/alphred
13 * @link http://shawnrice.github.io/alphred
14 * @since File available since Release 1.0.0
15 *
16 */
17
18 namespace Alphred;
19
20 /**
21 * Basic interaction with Alfred
22 */
23 class Alfred {
24
25 /**
26 * Calls an Alfred External Trigger
27 *
28 * Single and double-quotes in the argument might break this method, so make sure that you
29 * escape them appropriately.
30 *
31 * @since 1.0.0
32 *
33 * @param string $bundle the bundle id of the workflow to trigger
34 * @param string $trigger the name of the trigger
35 * @param string|boolean $argument an argument to pass
36 */
37 public static function call_external_trigger( $bundle, $trigger, $argument = false ) {
38 $script = "tell application \"Alfred 2\" to run trigger \"{$trigger}\" in workflow \"{$bundle}\"";
39 if ( false !== $argument ) {
40 $script .= "with argument \"{$argument}\"";
41 }
42 // Execute the AppleScript to call the trigger
43 exec( "osascript -e '$script'" );
44 }
45
46 /**
47 * Tells you if the current theme is `light` or `dark`
48 *
49 * @uses Alphred\Globals::get()
50 * @return string either 'light' or 'dark'
51 */
52 public static function light_or_dark() {
53 // Regex pattern to parse the Alfred background variable
54 $pattern = "/rgba\(([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9.]{4,})\)/";
55 // Do the regex, matching everything in the $matches variable
56 preg_match_all( $pattern, \Alphred\Globals::get( 'alfred_theme_background' ), $matches );
57 // Pull the values into an $rgb array
58 $rgb = array( 'r' => $matches[1][0], 'g' => $matches[2][0], 'b' => $matches[3][0] );
59
60 // This calculates the luminance. Values are between 0 and 1.
61 $luminance = ( 0.299 * $rgb[ 'r' ] + 0.587 * $rgb[ 'g' ] + 0.114 * $rgb[ 'b' ] ) / 255;
62
63 if ( 0.5 < $luminance ) {
64 return 'light';
65 }
66 return 'dark';
67 }
68
69 }