-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorscheme.php
87 lines (71 loc) · 2.14 KB
/
colorscheme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Plugin Colorscheme
*
* Displays a color in #xxyyzz format as an inline sample block.
*/
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\TemplatePage;
/* HSP Color Model - http://alienryderflex.com/hsp.html */
function dark_color($match)
{
$brightness = sqrt(.299 * pow(hexdec($match[1]), 2)
+ .587 * pow(hexdec($match[2]), 2)
+ .114 * pow(hexdec($match[3]), 2));
return ($brightness <= 127.5);
}
function display_color($match)
{
$hex_color = '#' . strtolower($match[1]) . strtolower($match[2]) . strtolower($match[3]);
$dark = dark_color($match) ? ' dark' : '';
return '<a class="colorscheme' . $dark
. '" style="background-color: ' . $hex_color
. '" href="#">' . $hex_color . '</a>';
}
function colorscheme_url($url)
{
$pattern = '/^https:\/\/assets.adobe.com\//i';
return preg_match($pattern, $url) ? 1 : 0;
}
function hook_colorscheme_render_editlink($data)
{
if ($data['link_is_new'] && colorscheme_url($data['link']['url'])) {
$data['link']['tags'] = 'colorscheme';
}
return $data;
}
function hook_colorscheme_render_linklist($data)
{
#$pattern = '/<a href="[^"]+">(#[a-f0-9]{6})</a>/i';
$pattern = '/<a href="[^"]+">#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})<\/a>(?:\r\n|\n)?(?:<br\s*\/?>)?/msi';
foreach ($data['links'] as &$value) {
$value['description'] = preg_replace_callback(
$pattern,
"display_color",
$value['description'],
-1,
$count
);
}
return $data;
}
function hook_colorscheme_render_includes($data)
{
if ($data['_PAGE_'] == TemplatePage::LINKLIST) {
$data['css_files'][] = PluginManager::$PLUGINS_PATH . '/colorscheme/colorscheme.css';
}
return $data;
}
function hook_colorscheme_render_footer($data)
{
$data['js_files'][] = PluginManager::$PLUGINS_PATH . '/colorscheme/colorscheme.js';
return $data;
}
/**
* This function is never called, but contains translation calls for GNU gettext extraction.
*/
function colorscheme_dummy_translation()
{
// meta
t('Displays a color in #xxyyzz format as an inline sample block.');
}