Skip to content

Commit

Permalink
Add lighten/darken capability to colorset expansion.
Browse files Browse the repository at this point in the history
  * Added expansion for appending .lighten<p> and .darken<p> to
    a color set expansion, such as $[fg.cs<n>.lighten<p>] or
    $[shadow.cs<n>.darken<p>].
  * Added missing $[fgsh.cs<n>] expansion to manpage.
  • Loading branch information
somiaj committed Apr 1, 2018
1 parent 4586227 commit e66b16a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Changes in stable release 2.6.8 (UNRELEASED)

- fvwm-menu-desktop(1) now requires python3 as an explicit
dependency.
- The colorset expansion parameters, $[fg.cs<n>], $[bg.cs<n>],
etc, can now expand p percent lighter or darker colors by
appending .lighten<p> or .darken<p> to the parameter.
For example, $[fg.cs3.lighten15] or $[shadow.cs3.darken15].

-------------------------------------------------------------------
Changes in stable release 2.6.7 (06-Mar-2016)
Expand Down
22 changes: 17 additions & 5 deletions doc/fvwm/expansion.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,27 @@ command can be used:</para>
$[bg.cs&lt;n&gt;]
$[hilight.cs&lt;n&gt;]
$[shadow.cs&lt;n&gt;]
$[fgsh.cs&lt;n&gt;]
</term>
<listitem>
<para>
These parameters are replaced with the name of the foreground
(fg), background (bg), hilight (hilight) or shadow (shadow) color
that is defined in colorset &lt;n&gt; (replace &lt;n&gt; with zero
or a positive integer). For example "$[fg.cs3]" is expanded to the
name of the foreground color of colorset 3 (in rgb:rrrr/gggg/bbbb
form). Please refer to the
(fg), background (bg), hilight (hilight), shadow (shadow), or
the font shadow (fgsh) color that is defined in colorset &lt;n&gt;
(replace &lt;n&gt; with zero or a positive integer). For example
"$[fg.cs3]" is expanded to the name of the foreground color of
colorset 3 (in rgb:rrrr/gggg/bbbb form).
</para>
<para>
In addition if .lighten&lt;p&gt; or .darken&lt;p&gt; is appeneded
to the parameters, they are instead replaced with a color that is
lighter or darker than the one defined in colorset &lt;n&gt;
by a percentage value &lt;p&gt; (between 0 and 100). For example
"$[bg.cs3.lighten15]" is expanded to the background color of
colorset 3 and then lightened 15% (in rgb:rrrr/gggg/bbbb form).
</para>
<para>
Please refer to the
<fvwmref sect="colorsets" opt="colorsets" name="Colorsets"/>
section for details about colorsets.
</para>
Expand Down
24 changes: 22 additions & 2 deletions fvwm/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ static signed int expand_vars_extended(
char dummy[64] = "\0";
char *target = (output) ? output : dummy;
int cs = -1;
int csadj = 0;
int nn = 0;
int n;
int i;
int l;
Expand All @@ -376,7 +378,7 @@ static signed int expand_vars_extended(
case VAR_HILIGHT_CS:
case VAR_SHADOW_CS:
case VAR_FGSH_CS:
if (!isdigit(*rest) || (*rest == '0' && *(rest + 1) != 0))
if (!isdigit(*rest) || (*rest == '0' && *(rest + 1) != 0 && *(rest + 1) != '.' ))
{
/* not a non-negative integer without leading zeros */
return -1;
Expand All @@ -386,6 +388,24 @@ static signed int expand_vars_extended(
return -1;
}
if (*(rest + n) != 0)
{
/* Check for .lightenN or .darkenN */
csadj = 101;
l = -1;
if (sscanf(rest + n, ".lighten%d%n", &l, &nn) && l >= 0 && l <= 100)
{
csadj = l;
}
if (sscanf(rest + n, ".darken%d%n", &l, &nn) && l >= 0 && l <= 100)
{
csadj = -l;
}
if (csadj == 101)
{
return -1;
}
}
if (*(rest + n + nn) != 0)
{
/* trailing characters */
return -1;
Expand Down Expand Up @@ -414,7 +434,7 @@ static signed int expand_vars_extended(
break;
}
is_target = True;
len = pixel_to_color_string(dpy, Pcmap, pixel, target, False);
len = pixel_to_color_string(dpy, Pcmap, pixel, target, False, csadj);
goto GOT_STRING;
case VAR_GT_:
if (rest == NULL)
Expand Down
18 changes: 17 additions & 1 deletion libs/ColorUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ Pixel GetTintedPixel(Pixel in, Pixel tint, int percent)
* memory area pointed at by 'output' must be at least 64 bytes (in case of
* future extensions and multibyte characters).*/
int pixel_to_color_string(
Display *dpy, Colormap cmap, Pixel pixel, char *output, Bool use_hash)
Display *dpy, Colormap cmap, Pixel pixel, char *output, Bool use_hash, int adj)
{
XColor color;
int n;
Expand All @@ -403,6 +403,22 @@ int pixel_to_color_string(
color.blue = 0;

XQueryColor(dpy, cmap, &color);

/* Lighten color */
if ( adj > 0 && adj <= 100 )
{
color.red = (int)( ((100 - adj)*color.red + adj*65535)/100 );
color.green = (int)( ((100 - adj)*color.green + adj*65535)/100 );
color.blue = (int)( ((100 - adj)*color.blue + adj*65535)/100 );
}
/* Darken color */
if ( adj >= -100 && adj < 0 )
{
color.red = (int)( (100 + adj)*color.red/100 );
color.green = (int)( (100 + adj)*color.green/100 );
color.blue = (int)( (100 + adj)*color.blue/100 );
}

if (!use_hash)
{
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion libs/ColorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Pixel GetTintedPixel(Pixel in, Pixel tint, int percent);
* memory area pointed at by 'output' must be at least 64 bytes (in case of
* future extensions and multibyte characters).*/
int pixel_to_color_string(
Display *dpy, Colormap cmap, Pixel pixel, char *output, Bool use_hash);
Display *dpy, Colormap cmap, Pixel pixel, char *output, Bool use_hash, int adj);

Pixel GetSimpleColor(char *name);
/* handles colorset color names too */
Expand Down

0 comments on commit e66b16a

Please sign in to comment.