Page MenuHomePhorge

D25540.1739170424.diff
No OneTemporary

D25540.1739170424.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/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,53 @@
+<?php
+
+final class PhutilRemarkupHexColorCodeRule
+ extends PhabricatorRemarkupCustomInlineRule {
+
+ public function getPriority() {
+ return 1000.0;
+ }
+
+
+ protected function contrastingColor($color_code) {
+ $match = ltrim($color_code, '#');
+ $colors_hex = str_split($match, 2);
+ 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';
+ }
+
+ public function apply($text) {
+
+ return preg_replace_callback(
+ '@\B\{(#[0-9a-fA-F]{6,})\}@',
+ array($this, 'markupHexColorCodedText'),
+ $text);
+ }
+
+ protected function markupHexColorCodedText(array $matches) {
+ if ($this->getEngine()->isTextMode()) {
+ $result = $matches[1];
+ } else {
+ if (count($matches) < 2 || 7 !== strlen($matches[1])) {
+ 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();
@@ -98,7 +99,6 @@
$rules[] = new PhutilRemarkupDelRule();
$rules[] = new PhutilRemarkupUnderlineRule();
$rules[] = new PhutilRemarkupHighlightRule();
-
$blocks = array();
$blocks[] = new PhutilRemarkupQuotesBlockRule();
$blocks[] = new PhutilRemarkupReplyBlockRule();
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}
+~~~~~~~~~~
+<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></p>
+~~~~~~~~~~
+some red, grey and white for you: #ff0000 #999999 #ffffff

File Metadata

Mime Type
text/plain
Expires
Mon, Feb 10, 06:53 (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1153645
Default Alt Text
D25540.1739170424.diff (4 KB)

Event Timeline