Skip to content

Introduce new getThemeBackgroundColor utility function#69

Merged
allejo merged 8 commits into
masterfrom
feature/theme-bg-colors
Mar 2, 2020
Merged

Introduce new getThemeBackgroundColor utility function#69
allejo merged 8 commits into
masterfrom
feature/theme-bg-colors

Conversation

@allejo

@allejo allejo commented Feb 29, 2020

Copy link
Copy Markdown
Collaborator

Requested in westonruter/syntax-highlighting-code-block#20, this PR introduces a new utility function named getThemeBackgroundColor that will return the hexadecimal of the background used in a given theme.

Is this what you had in mind, @westonruter? Introducing any color operations to perform on said hexadecimal values would be beyond the scope of this project. So those calculations would be left up to the WP plugin.

Add CSS parser as a dev dependency to be able to retrieve the
hexadecimal of a theme's background color.

@westonruter westonruter left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this would serve well to be able to compute the default text highlight color, don't you? Given a dark background color, we'd presume a light text color, and thus a darker color for highlighting the text would be required. Otherwise, given a light background color, then the text would be assumed to be dark, and the highlight color should be light. I don't know what those specific colors should be, but this seems like what is required to inform that.

Comment thread tools/get_styles_colors.php Outdated
PHP;

$document = strtr($document, array(
'{colorMapping}' => implode("\n", $backgroundColors),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: Why not use var_export() instead of manually constructing the array above?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only because I didn't know var_export() existed! TIL

Comment thread tools/get_styles_colors.php Outdated
/** @var Rule $value */
foreach ($bgColor as $value) {
if ($value->getValue() instanceof Color) {
$color = preg_replace('/background(?:-color)?:\s|;/', '', (string) $value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be preferable to use \Sabberworm\CSS\Value\Color::getColor() here so that the underlying RGB colors can be returned (as an array), as opposed to a hexadecimal representation (as a string).

Suggested change
$color = preg_replace('/background(?:-color)?:\s|;/', '', (string) $value);
$color = $value->getValue()->getColor();

This will avoid the need to re-parse the hex string, and the values can be passed straight into an rgb() function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this much better. I'm returning an array of numerical RGB values now instead.

Comment thread HighlightUtilities/_themeColors.php Outdated
'tomorrow-night-bright' => array(
0 => 255,
1 => 255,
2 => 255,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently #fff is getting parser without the r g b keys. Perhaps this should get normalized for highlight.php?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh oops, it wasn't that it was being parsed incorrectly, I just gave it the wrong default values when no color was given. Technically it's transparent but I'm just assigning it white. Should we indicate transparent somehow?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose a default of white is correct, since that is the browser default. That is unless there are dark schemes that also have transparent backgrounds?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any dark themes have a transparent background. I was only thinking about @media(prefers-color-scheme) but I think at that point, it should be up to the theme designer to get colors right.

@allejo allejo force-pushed the feature/theme-bg-colors branch from eab8ebc to 6a348c6 Compare March 2, 2020 01:39
@allejo allejo force-pushed the feature/theme-bg-colors branch from 96f8a44 to 38fe996 Compare March 2, 2020 05:35
@allejo allejo merged commit 52fc21c into master Mar 2, 2020
@allejo allejo deleted the feature/theme-bg-colors branch March 2, 2020 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants