Skip to content

Commit

Permalink
FIX comment color and create colorIsLight function
Browse files Browse the repository at this point in the history
  • Loading branch information
atm-arnaud committed Sep 13, 2017
1 parent adc7829 commit 4b6553d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
24 changes: 2 additions & 22 deletions htdocs/core/class/html.formother.class.php
Expand Up @@ -601,29 +601,9 @@ private function _pLineSelect(&$inc, $parent, $lines, $level=0, $selectedtask=0,
static function showColor($color, $textifnotdefined='')
{
$textcolor='FFF';
if ($color)
{
$tmp=explode(',', $color);
if (count($tmp) > 1) // This is a comma RGB ('255','255','255')
{
$r = $tmp[0];
$g = $tmp[1];
$b = $tmp[2];
}
else
{
$hexr=$color[0].$color[1];
$hexg=$color[2].$color[3];
$hexb=$color[4].$color[5];
$r = hexdec($hexr);
$g = hexdec($hexg);
$b = hexdec($hexb);
}
$bright = (max($r, $g, $b) + min($r, $g, $b)) / 510.0; // HSL algorithm
if ($bright > 0.6) $textcolor='000';
}

include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
if(colorIsLight($color)) $textcolor='000';

$color = colorArrayToHex(colorStringToArray($color,array()),'');

if ($color) print '<input type="text" class="colorthumb" disabled style="padding: 1px; margin-top: 0; margin-bottom: 0; color: #'.$textcolor.'; background-color: #'.$color.'" value="'.$color.'">';
Expand Down
34 changes: 34 additions & 0 deletions htdocs/core/lib/functions2.lib.php
Expand Up @@ -2145,6 +2145,40 @@ function colorStringToArray($stringcolor,$colorifnotfound=array(88,88,88))
return array(hexdec($reg[1]),hexdec($reg[2]),hexdec($reg[3]));
}

/**
* Return true if the color is light
*
* @param string $stringcolor String with hex (FFFFFF) or comma RGB ('255,255,255')
* @return int -1 : Error with argument passed |0 : color is dark | 1 : color is light
*/
function colorIsLight($stringcolor)
{
$res = -1;
if (!empty($stringcolor))
{
$res = 0;
$tmp=explode(',', $stringcolor);
if (count($tmp) > 1) // This is a comma RGB ('255','255','255')
{
$r = $tmp[0];
$g = $tmp[1];
$b = $tmp[2];
}
else
{
$hexr=$stringcolor[0].$stringcolor[1];
$hexg=$stringcolor[2].$stringcolor[3];
$hexb=$stringcolor[4].$stringcolor[5];
$r = hexdec($hexr);
$g = hexdec($hexg);
$b = hexdec($hexb);
}
$bright = (max($r, $g, $b) + min($r, $g, $b)) / 510.0; // HSL algorithm
if ($bright > 0.6) $res = 1;
}
return $res;
}

/**
* Applies the Cartesian product algorithm to an array
* Source: http://stackoverflow.com/a/15973172
Expand Down
15 changes: 11 additions & 4 deletions htdocs/projet/tasks/comment.php
Expand Up @@ -348,20 +348,25 @@
// List of comments
if(!empty($task->comments)) {
// Default color for current user
$TColors = array($user->id => 'efefef');
$TColors = array($user->id => array('bgcolor'=>'efefef','color'=>'555'));
$first = true;
foreach($task->comments as $comment) {
$fk_user = $comment->fk_user;
$userstatic->fetch($fk_user);
if(empty($TColors[$fk_user])) {
$TColors[$fk_user] = random_color(180,240);
$bgcolor = random_color(180,240);
if(!empty($userstatic->color)) {
$bgcolor = $userstatic->color;
}
$color = (colorIsLight($bgcolor))?'555':'fff';
$TColors[$fk_user] = array('bgcolor'=>$bgcolor,'color'=>$color);
}
print '<div class="width100p clearboth">';
print '<div class="width100p" style="color:#'.$TColors[$fk_user]['color'].'">';
if($comment->fk_user == $user->id) {
print '<div class="width25p float">&nbsp;</div>';
}

print '<div class="width75p float comment" style="background-color:#'.$TColors[$fk_user].'">';
print '<div class="width75p float comment" style="background-color:#'.$TColors[$fk_user]['bgcolor'].'">';
print '<div class="comment-description">';
print $comment->description;
print '</div>';
Expand All @@ -377,6 +382,8 @@
if($comment->fk_user != $user->id) {
print '<div class="width25p float">&nbsp;</div>';
}
print '<div class="clearboth"></div>';
print '</div>';

$first = false;
}
Expand Down
4 changes: 3 additions & 1 deletion htdocs/theme/eldy/style.css.php
Expand Up @@ -3825,9 +3825,11 @@
}
#comment .comment-info {
font-size:0.8em;
color:#555;
margin-top:5px;
}
#comment .comment-info a {
color:inherit;
}
#comment textarea {
width: 100%;
}
Expand Down

0 comments on commit 4b6553d

Please sign in to comment.