Skip to content

Commit db242ac

Browse files
tag text color contrast from background color
tags data are now stored in JSON file CSS file is just an output
1 parent 1aaeec8 commit db242ac

File tree

8 files changed

+181
-185
lines changed

8 files changed

+181
-185
lines changed

app/class/Color.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Wcms;
4+
5+
use DomainException;
6+
use RangeException;
7+
8+
class Color
9+
{
10+
public int $r;
11+
public int $g;
12+
public int $b;
13+
14+
/**
15+
* @param int $r red value, stored from 0 to 255
16+
* @param int $g green value, stored from 0 to 255
17+
* @param int $b blue value, stored from 0 to 255
18+
*
19+
* @throws DomainException If RGB values are not between 0 and 255.
20+
*/
21+
public function __construct(int $r, int $g, int $b)
22+
{
23+
if ($r > 255 || $r < 0 || $g > 255 || $g < 0 || $b > 255 || $b < 0) {
24+
throw new DomainException("Invalid rgb value: $r, $g, $b to define Color object");
25+
}
26+
$this->r = $r;
27+
$this->g = $g;
28+
$this->b = $b;
29+
}
30+
31+
/**
32+
* @return int Luma value from 0 to 255
33+
*/
34+
public function luma(): int
35+
{
36+
return ($this->r * 299 + $this->g * 587 + $this->b * 114) / 1000;
37+
}
38+
39+
/**
40+
* @return string hexa color code starting with #
41+
*/
42+
public function hexa(): string
43+
{
44+
$hex = '#';
45+
$hex .= str_pad(dechex($this->r), 2, '0', STR_PAD_LEFT);
46+
$hex .= str_pad(dechex($this->g), 2, '0', STR_PAD_LEFT);
47+
$hex .= str_pad(dechex($this->b), 2, '0', STR_PAD_LEFT);
48+
return $hex;
49+
}
50+
}

app/class/Colors.php

Lines changed: 0 additions & 172 deletions
This file was deleted.

app/class/Controllerhome.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public function desktop()
5050
$this->opt->submit();
5151

5252
try {
53-
$vars['colors'] = new Colors(Model::COLORS_FILE, $this->opt->taglist());
53+
$servicetags = new Servicetags();
54+
$vars['colors'] = $servicetags->synctags($this->opt->taglist());
5455
} catch (RuntimeException $e) {
5556
Model::sendflashmessage("Error while generating display colors", Model::FLASH_ERROR);
57+
Logger::errorex($e);
5658
}
5759

5860
$publicbookmarks = $this->bookmarkmanager->getlisterpublic();
@@ -203,12 +205,13 @@ public function columns()
203205

204206
public function colors()
205207
{
206-
if (isset($_POST['tagcolor']) && $this->user->issupereditor()) {
208+
if ($this->user->issupereditor()) {
207209
try {
208-
$colors = new Colors(Model::COLORS_FILE);
209-
$colors->update($_POST['tagcolor']);
210+
$servicetags = new Servicetags();
211+
$servicetags->updatecolors($_POST);
210212
} catch (RuntimeException $e) {
211213
Model::sendflashmessage("Error while saving display colors", Model::FLASH_ERROR);
214+
Logger::errorex($e);
212215
}
213216
}
214217
$this->routedirect('home');

app/class/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract class Model
1010
public const ASSETS_CSS_DIR = 'assets/css/';
1111
public const ASSETS_ATOM_DIR = 'assets/atom/';
1212
public const COLORS_FILE = self::ASSETS_CSS_DIR . 'tagcolors.css';
13+
public const TAGS_FILE = self::DATABASE_DIR . 'tags.json';
1314
public const THEME_DIR = self::ASSETS_CSS_DIR . 'theme/';
1415
public const JS_DIR = 'assets/js/';
1516
public const ICONS_DIR = 'assets/icons/';

app/class/Opt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ public function linkto(): string
357357
return $this->linkto;
358358
}
359359

360+
/**
361+
* @return int[] Array where keys are tags and value the number of pages
362+
*/
360363
public function taglist(): array
361364
{
362365
return $this->taglist;

app/class/Servicetags.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Wcms;
4+
5+
use Exception;
6+
7+
class Servicetags
8+
{
9+
/**
10+
* @return string[] List of tags
11+
*
12+
* @throws Fileexception If a problem with tag file occured
13+
*/
14+
public function taglist(): array
15+
{
16+
try {
17+
$tagfile = Fs::readfile(Model::TAGS_FILE);
18+
} catch (Notfoundexception $e) {
19+
// This mean the tag fille does not exist
20+
return [];
21+
}
22+
$tagdata = json_decode($tagfile, true);
23+
return array_keys($tagdata);
24+
}
25+
26+
/**
27+
* Update the tag JSON cache file with the last tag list.
28+
* This also re-generate the CSS file.
29+
*
30+
* @param array $tags Array of tags as keys
31+
*
32+
* @return array[] Array of tags as keys and two sub-arrays: background-color and color
33+
*
34+
* @throws Filesystemexception If an error occured with file reading or saving
35+
*/
36+
public function synctags(array $tags): array
37+
{
38+
try {
39+
$tagfile = Fs::readfile(Model::TAGS_FILE);
40+
$tagdata = json_decode($tagfile, true);
41+
$tagdata = array_intersect_key($tagdata, $tags);
42+
$newtags = array_diff_key($tags, $tagdata);
43+
if (empty($newtags)) {
44+
return $tagdata;
45+
}
46+
} catch (Notfoundexception $e) {
47+
// it means tags.json file did not exist, not a big deal
48+
$newtags = $tags;
49+
$tagdata = [];
50+
}
51+
foreach ($newtags as $tag => $count) {
52+
$bgcolor = new Color(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
53+
$txtcolor = $bgcolor->luma() > 128 ? 'black' : 'white';
54+
$tagdata[$tag] = [
55+
'background-color' => $bgcolor->hexa(),
56+
'color' => $txtcolor
57+
];
58+
}
59+
Fs::writefile(Model::TAGS_FILE, json_encode($tagdata, JSON_PRETTY_PRINT));
60+
$this->generatecssfile($tagdata);
61+
return $tagdata;
62+
}
63+
64+
/**
65+
* update tag colors
66+
* This also re-generate the CSS file.
67+
*
68+
* @throws Filesystemexception When error occured with writing files
69+
*/
70+
public function updatecolors($post): void
71+
{
72+
$tagdata = [];
73+
foreach ($post as $tag => $color) {
74+
$color = sscanf($color, "#%02x%02x%02x");
75+
$bgcolor = new Color($color[0], $color[1], $color[2]);
76+
$txtcolor = $bgcolor->luma() > 128 ? 'black' : 'white';
77+
$tagdata[$tag] = [
78+
'background-color' => $bgcolor->hexa(),
79+
'color' => $txtcolor
80+
];
81+
}
82+
Fs::writefile(Model::TAGS_FILE, json_encode($tagdata, JSON_PRETTY_PRINT));
83+
$this->generatecssfile($tagdata);
84+
}
85+
86+
/**
87+
* Generate CSS file according to tag datas
88+
*
89+
* @param array $tagdata Array of tags as keys and two sub-arrays: background-color and color
90+
*
91+
* @throws Filesystemexception If an error while saving CSS file
92+
*/
93+
protected function generatecssfile(array $tagdata): void
94+
{
95+
$css = '';
96+
foreach ($tagdata as $tag => $datas) {
97+
$bgcolor = $datas['background-color'];
98+
$color = $datas['color'];
99+
$css .= "\n.tag_$tag { background-color: $bgcolor; color: $color; }";
100+
}
101+
Fs::writefile(Model::COLORS_FILE, $css, 0664);
102+
}
103+
}

app/view/templates/homemenu.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,18 @@
445445
<input type="submit" value="update columns">
446446
</form>
447447

448-
<?php if($user->issupereditor() && !empty($colors)) { ?>
448+
<?php if($user->issupereditor()) { ?>
449449
<h2>Tag colors</h2>
450450
<form action="<?= $this->url('homecolors') ?>" method="post">
451-
<?= $colors->htmlcolorpicker() ?>
452-
<input type="submit" value="update">
451+
<ul>
452+
<?php foreach ($colors as $tag => $datas) { ?>
453+
<li>
454+
<input type="color" name="<?= $tag ?>" value="<?= $datas['background-color'] ?>" id="color_<?= $tag ?>">
455+
<label for="color_<?= $tag ?>"><?= $tag ?></label>
456+
</li>
457+
<?php } ?>
458+
</ul>
459+
<input type="submit" value="update tag colors">
453460
</form>
454461
<?php } ?>
455462
</div>

0 commit comments

Comments
 (0)