@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add PhutilRemarkupHexColorCodeRule, a new remarkup rule to format color codes

Summary:
Implements a remarkup rule that formats the background color to match the value of inline hex color codes.

The format for this rule looks like `{#RRGGBB}` and it will be formatted the same as monospace text, so `#RRGGBB` with the background set to the specified color.

Test Plan:
* run `arc unit` and see the tests pass
* create some markup with {#ff0000} color codes for all of your favorite colors {#ee33ee}

Reviewers: O1 Blessed Committers, avivey

Reviewed By: O1 Blessed Committers, avivey

Subscribers: avivey, aklapper, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15744

Differential Revision: https://we.phorge.it/D25540

+66 -1
+2
src/__phutil_library_map__.php
··· 5750 5750 'PhutilRemarkupEscapeRemarkupRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEscapeRemarkupRule.php', 5751 5751 'PhutilRemarkupEvalRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php', 5752 5752 'PhutilRemarkupHeaderBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHeaderBlockRule.php', 5753 + 'PhutilRemarkupHexColorCodeRule' => 'infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php', 5753 5754 'PhutilRemarkupHighlightRule' => 'infrastructure/markup/markuprule/PhutilRemarkupHighlightRule.php', 5754 5755 'PhutilRemarkupHorizontalRuleBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHorizontalRuleBlockRule.php', 5755 5756 'PhutilRemarkupHyperlinkEngineExtension' => 'infrastructure/markup/markuprule/PhutilRemarkupHyperlinkEngineExtension.php', ··· 12637 12638 'PhutilRemarkupEscapeRemarkupRule' => 'PhutilRemarkupRule', 12638 12639 'PhutilRemarkupEvalRule' => 'PhutilRemarkupRule', 12639 12640 'PhutilRemarkupHeaderBlockRule' => 'PhutilRemarkupBlockRule', 12641 + 'PhutilRemarkupHexColorCodeRule' => 'PhabricatorRemarkupCustomInlineRule', 12640 12642 'PhutilRemarkupHighlightRule' => 'PhutilRemarkupRule', 12641 12643 'PhutilRemarkupHorizontalRuleBlockRule' => 'PhutilRemarkupBlockRule', 12642 12644 'PhutilRemarkupHyperlinkEngineExtension' => 'Phobject',
+1 -1
src/infrastructure/markup/PhabricatorMarkupEngine.php
··· 526 526 $rules[] = new PhutilRemarkupEscapeRemarkupRule(); 527 527 $rules[] = new PhutilRemarkupEvalRule(); 528 528 $rules[] = new PhutilRemarkupMonospaceRule(); 529 - 529 + $rules[] = new PhutilRemarkupHexColorCodeRule(); 530 530 531 531 $rules[] = new PhutilRemarkupDocumentLinkRule(); 532 532 $rules[] = new PhabricatorNavigationRemarkupRule();
+57
src/infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php
··· 1 + <?php 2 + 3 + final class PhutilRemarkupHexColorCodeRule 4 + extends PhabricatorRemarkupCustomInlineRule { 5 + 6 + public function getPriority() { 7 + return 1000.0; 8 + } 9 + 10 + public function apply($text) { 11 + // Match {#FFFFFF} 12 + return preg_replace_callback( 13 + '@\B\{(#([0-9a-fA-F]{3}){1,2})\}@', 14 + array($this, 'markupHexColorCodedText'), 15 + $text); 16 + } 17 + 18 + protected function contrastingColor($color_code) { 19 + $match = ltrim($color_code, '#'); 20 + $colors_hex = str_split($match, strlen($match) / 3); 21 + list($r, $g, $b) = array_map('hexdec', $colors_hex); 22 + // Calculation adapted from Myndex, CC BY-SA 4.0 23 + // https://stackoverflow.com/a/69869976 24 + $y = pow((double)$r / 255.0, 2.2) * 0.2126 + 25 + pow((double)$g / 255.0, 2.2) * 0.7152 + 26 + pow((double)$b / 255.0, 2.2) * 0.0722; 27 + 28 + return ($y < 0.34) ? 'white' : 'black'; 29 + } 30 + 31 + protected function markupHexColorCodedText(array $matches) { 32 + if ($this->getEngine()->isTextMode()) { 33 + $result = $matches[1]; 34 + } else { 35 + if (count($matches) < 2) { 36 + return $matches[0]; 37 + } else { 38 + $len = strlen($matches[1]); 39 + if (7 !== $len && 4 !== $len) { 40 + return $matches[0]; 41 + } 42 + } 43 + $match = $matches[1]; 44 + $fg = $this->contrastingColor($match); 45 + $result = phutil_tag( 46 + 'tt', 47 + array( 48 + 'class' => 'remarkup-monospaced', 49 + 'style' => "color: {$fg}; background-color: {$match};", 50 + ), 51 + $match); 52 + } 53 + 54 + return $this->getEngine()->storeText($result); 55 + } 56 + 57 + }
+1
src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
··· 91 91 $rules = array(); 92 92 $rules[] = new PhutilRemarkupEscapeRemarkupRule(); 93 93 $rules[] = new PhutilRemarkupMonospaceRule(); 94 + $rules[] = new PhutilRemarkupHexColorCodeRule(); 94 95 $rules[] = new PhutilRemarkupDocumentLinkRule(); 95 96 $rules[] = new PhutilRemarkupHyperlinkRule(); 96 97 $rules[] = new PhutilRemarkupBoldRule();
+5
src/infrastructure/markup/remarkup/__tests__/remarkup/hex-color-code.txt
··· 1 + some red, grey and white for you: {#ff0000} {#999999} {#ffffff} Also green: {#0F0} but not this: {#0000000} or this: {#0000} 2 + ~~~~~~~~~~ 3 + <p>some red, grey and white for you: <tt class="remarkup-monospaced" style="color: white; background-color: #ff0000;">#ff0000</tt> <tt class="remarkup-monospaced" style="color: white; background-color: #999999;">#999999</tt> <tt class="remarkup-monospaced" style="color: black; background-color: #ffffff;">#ffffff</tt> Also green: <tt class="remarkup-monospaced" style="color: white; background-color: #0F0;">#0F0</tt> but not this: {#0000000} or this: {#0000}</p> 4 + ~~~~~~~~~~ 5 + some red, grey and white for you: #ff0000 #999999 #ffffff Also green: #0F0 but not this: {#0000000} or this: {#0000}