Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40339

Re: Help with regexp

Path csiph.com!tncsrv06.tnetconsulting.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date Sun, 08 Aug 2021 22:13:43 -0500
Subject Re: Help with regexp
Newsgroups comp.lang.javascript
References <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <3136550.aeNJFYEL58@PointedEars.de>
From Andrew Poulos <ap_prog@hotmail.com>
Date Mon, 9 Aug 2021 13:13:40 +1000
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0
MIME-Version 1.0
In-Reply-To <3136550.aeNJFYEL58@PointedEars.de>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Language en-AU
Content-Transfer-Encoding 8bit
Message-ID <F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au> (permalink)
Lines 62
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 110.175.24.76
X-Trace sv3-3qC+pgIdow1NySn6h2oSYJqPxmnQeSkVJ3rb0awcUm6/mmybZG65o26/iZ3ebdf+/JjymCTbaQjnpA7!4TD7difs/TdNaPpQZm4S4cPQ5J0+Qzo1nxrVRrPyFk3BmFNdTr9+A2RlQTStyCPilX9IKL9WqxNq!1e/Zm7g2wmb0O1iC0Llg
X-Complaints-To abuse@westnet.com.au
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 3252
Xref csiph.com comp.lang.javascript:40339

Show key headers only | View raw


On 8/08/2021 5:56 am, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
> 
>> I have a string that looks like this
>>
>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>
>> I need to convert the hex values to rgba so I've written a function
>>
>> const convertHex2Rgba = (hex, alpha = 1) => {
>>     ...
>>     return `rgba(${r},${g},${b},${alpha})`;
>> };
>>
>> I'm stuck trying to write a regexp that replaces the hex values (that
>> may have either 3 or 6 characters - plus the #) with the value returned
>> from convertHex2Rgba.
>>
>> This (one of my many failed attempts):
>>
>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>
>> does nothing.
> 
> Of course not.
> 
>    1. You are not using the return value of the “replace” method.
> 
>    2. You are not passing your function a proper value for the formal
>       parameter “hex”, but the string value '$1' instead.
>       ('$1' is only expanded with the first captured subpattern if
>        the entire argument is a string value.)
> 
>    3. You are not matching that which you intend to match.
>       (You are only matching a single hexadecimal digit.)
> 
> This should work:
> 
>    const generateHex2Rgba =
>      ((alpha = 1) => ((hex, red, green, blue, count) => {
>        let [r, g, b] = [red, green, blue].map(s => parseInt(s, 16));
>        return `rgba(${r},${g},${b},${alpha})`;
>      }));

Nice and concise code but what is 'count' for?

>    str = str.replace(
>      /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,
>      generateHex2Rgba()
>    );
> 
> Why you want to do that remains a mystery, though.

The hex values are from gradient backgrounds that users are grabbing 
from online CSS generators (most, if not all, of them return colours in 
hex). The backgrounds need to animate so I'm using anime.js to "play" 
with the various colour and alpha values. This is easier for me with 
rgba than (hex or hexa).

Andrew Poulos

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:22 +1000
  Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:41 +1000
    Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:55 +1000
      Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 01:39 -0700
        Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 01:53 -0700
        Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-07 09:19 +1000
          Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 17:26 -0700
      Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-06 10:36 +0000
        Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-07 09:17 +1000
          Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-07 18:57 +0000
  Re: Help with regexp The Natural Philosopher <tnp@invalid.invalid> - 2021-08-06 19:10 +0100
  Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-07 21:56 +0200
    Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-07 22:02 +0200
    Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-09 13:13 +1000
      Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-10 13:45 +0200
        Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-10 12:28 +0000
          Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-11 16:03 +0200

csiph-web