Page MenuHomePhorge

D25540.1736648009.diff
No OneTemporary

D25540.1736648009.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -5750,6 +5750,7 @@
'PhutilRemarkupEscapeRemarkupRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEscapeRemarkupRule.php',
'PhutilRemarkupEvalRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php',
'PhutilRemarkupHeaderBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHeaderBlockRule.php',
+ 'PhutilRemarkupHexColorCodeRule' => 'infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php',
'PhutilRemarkupHighlightRule' => 'infrastructure/markup/markuprule/PhutilRemarkupHighlightRule.php',
'PhutilRemarkupHorizontalRuleBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHorizontalRuleBlockRule.php',
'PhutilRemarkupHyperlinkEngineExtension' => 'infrastructure/markup/markuprule/PhutilRemarkupHyperlinkEngineExtension.php',
@@ -12637,6 +12638,7 @@
'PhutilRemarkupEscapeRemarkupRule' => 'PhutilRemarkupRule',
'PhutilRemarkupEvalRule' => 'PhutilRemarkupRule',
'PhutilRemarkupHeaderBlockRule' => 'PhutilRemarkupBlockRule',
+ 'PhutilRemarkupHexColorCodeRule' => 'PhabricatorRemarkupCustomInlineRule',
'PhutilRemarkupHighlightRule' => 'PhutilRemarkupRule',
'PhutilRemarkupHorizontalRuleBlockRule' => 'PhutilRemarkupBlockRule',
'PhutilRemarkupHyperlinkEngineExtension' => 'Phobject',
diff --git a/src/infrastructure/markup/PhabricatorMarkupEngine.php b/src/infrastructure/markup/PhabricatorMarkupEngine.php
--- a/src/infrastructure/markup/PhabricatorMarkupEngine.php
+++ b/src/infrastructure/markup/PhabricatorMarkupEngine.php
@@ -526,7 +526,7 @@
$rules[] = new PhutilRemarkupEscapeRemarkupRule();
$rules[] = new PhutilRemarkupEvalRule();
$rules[] = new PhutilRemarkupMonospaceRule();
-
+ $rules[] = new PhutilRemarkupHexColorCodeRule();
$rules[] = new PhutilRemarkupDocumentLinkRule();
$rules[] = new PhabricatorNavigationRemarkupRule();
diff --git a/src/infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php b/src/infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php
new file mode 100644
--- /dev/null
+++ b/src/infrastructure/markup/markuprule/PhutilRemarkupHexColorCodeRule.php
@@ -0,0 +1,57 @@
+<?php
+
+final class PhutilRemarkupHexColorCodeRule
+ extends PhabricatorRemarkupCustomInlineRule {
+
+ public function getPriority() {
+ return 1000.0;
+ }
+
+ public function apply($text) {
+ // Match {#FFFFFF}
+ return preg_replace_callback(
+ '@\B\{(#([0-9a-fA-F]{3}){1,2})\}@',
+ array($this, 'markupHexColorCodedText'),
+ $text);
+ }
+
+ protected function contrastingColor($color_code) {
+ $match = ltrim($color_code, '#');
+ $colors_hex = str_split($match, strlen($match) / 3);
+ list($r, $g, $b) = array_map('hexdec', $colors_hex);
+ // Calculation adapted from Myndex, CC BY-SA 4.0
+ // https://stackoverflow.com/a/69869976
+ $y = pow((double)$r / 255.0, 2.2) * 0.2126 +
+ pow((double)$g / 255.0, 2.2) * 0.7152 +
+ pow((double)$b / 255.0, 2.2) * 0.0722;
+
+ return ($y < 0.34) ? 'white' : 'black';
+ }
+
+ protected function markupHexColorCodedText(array $matches) {
+ if ($this->getEngine()->isTextMode()) {
+ $result = $matches[1];
+ } else {
+ if (count($matches) < 2) {
+ return $matches[0];
+ } else {
+ $len = strlen($matches[1]);
+ if (7 !== $len && 4 !== $len) {
+ return $matches[0];
+ }
+ }
+ $match = $matches[1];
+ $fg = $this->contrastingColor($match);
+ $result = phutil_tag(
+ 'tt',
+ array(
+ 'class' => 'remarkup-monospaced',
+ 'style' => "color: {$fg}; background-color: {$match};",
+ ),
+ $match);
+ }
+
+ return $this->getEngine()->storeText($result);
+ }
+
+}
diff --git a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
--- a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
+++ b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
@@ -91,6 +91,7 @@
$rules = array();
$rules[] = new PhutilRemarkupEscapeRemarkupRule();
$rules[] = new PhutilRemarkupMonospaceRule();
+ $rules[] = new PhutilRemarkupHexColorCodeRule();
$rules[] = new PhutilRemarkupDocumentLinkRule();
$rules[] = new PhutilRemarkupHyperlinkRule();
$rules[] = new PhutilRemarkupBoldRule();
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/hex-color-code.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/hex-color-code.txt
new file mode 100644
--- /dev/null
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/hex-color-code.txt
@@ -0,0 +1,5 @@
+some red, grey and white for you: {#ff0000} {#999999} {#ffffff} Also green: {#0F0} but not this: {#0000000} or this: {#0000}
+~~~~~~~~~~
+<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>
+~~~~~~~~~~
+some red, grey and white for you: #ff0000 #999999 #ffffff Also green: #0F0 but not this: {#0000000} or this: {#0000}

File Metadata

Mime Type
text/plain
Expires
Sun, Jan 12, 02:13 (2 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1111678
Default Alt Text
D25540.1736648009.diff (5 KB)

Event Timeline