Skip to content

Dark color improvement#3279

Open
JiuqingSong wants to merge 2 commits intomasterfrom
u/jisong/darkcolorimprovement
Open

Dark color improvement#3279
JiuqingSong wants to merge 2 commits intomasterfrom
u/jisong/darkcolorimprovement

Conversation

@JiuqingSong
Copy link
Collaborator

@JiuqingSong JiuqingSong commented Feb 5, 2026

When call color callback to calculate dark color, pass in the text color for background color so the callback function can calculate a better background color according to the text color.

And in order to allow different background dark color for different text color, also add text color to background color key.
Example:
old color key: --darkColor__f5d427 (here #f5d427 is the light mode background color)
new color key: --darkColor__f5d427__ff0000 (here #ff0000 is the light mode text color)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves dark-mode background color generation by letting the dark color callback consider the associated text color, and by incorporating the text color into the generated dark color CSS variable key so different foreground/background pairings can produce different dark backgrounds.

Changes:

  • Extend dark color transformation APIs to accept an optional comparingColor (intended to be text color when generating dark backgrounds).
  • Update dark color key generation to include comparingColor (e.g. --darkColor_<bg>_<text>), and propagate inherited text color during DOM color transformation.
  • Update and add tests to reflect the new key format and call signatures.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/roosterjs-editor-adapter/lib/editor/EditorAdapter.ts Passes editor default text color into transformColor during dark color transformation.
packages/roosterjs-content-model-types/lib/context/DarkColorHandler.ts Extends ColorTransformFunction signature to include optional comparingColor.
packages/roosterjs-content-model-dom/lib/formatHandlers/utils/color.ts Threads comparingColor through setColor/adaptColor and updates default key generation to append it.
packages/roosterjs-content-model-dom/lib/formatHandlers/common/backgroundColorFormatHandler.ts Passes text color as comparingColor when applying background color in dark mode.
packages/roosterjs-content-model-dom/lib/domUtils/style/transformColor.ts Propagates parent/inherited text color to improve background dark color generation and keying.
packages/roosterjs-content-model-core/lib/editor/Editor.ts Passes editor default text color into transformColor when toggling dark mode.
packages/roosterjs-content-model-dom/test/formatHandlers/utils/colorTest.ts Updates expectations for new key format and getDarkColor argument list.
packages/roosterjs-content-model-dom/test/formatHandlers/common/backgroundColorFormatHandlerTest.ts Adds coverage for combined text+background in dark mode affecting background key.
packages/roosterjs-content-model-dom/test/domUtils/style/transformColorTest.ts Updates expected background keys and adds inherited text color scenario coverage.
packages/roosterjs-content-model-core/test/editor/EditorTest.ts Updates transformColor call expectations for the new optional parameter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +9 to +11
export const backgroundColorFormatHandler: FormatHandler<
BackgroundColorFormat & TextColorFormat
> = {
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

backgroundColorFormatHandler is now typed as FormatHandler<BackgroundColorFormat & TextColorFormat>, but the default format handler registry expects FormatHandler<BackgroundColorFormat> (see FormatHandlerTypeMap.backgroundColor). This can cause a TypeScript assignability error (or force callers/tests to widen types) even though textColor is only used as an optional hint.

Suggested fix: keep the handler typed as FormatHandler<BackgroundColorFormat> and read comparing text color via a safe cast (e.g., const comparingColor = (format as Partial<TextColorFormat>).textColor ?? context.implicitFormat.textColor) when calling setColor.

Copilot uses AI. Check for mistakes.
* @param baseLValue Base value of light used for dark value calculation
* @param colorType @optional Type of color, can be text, background, or border
* @param element @optional Source HTML element of the color
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark border color has enough contrast with text color in dark mode
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The new comparingColor param doc says it helps ensure the generated "dark border color" has enough contrast, but this type is used for generating dark colors in general and the param is specifically described as for background-vs-text contrast. Please update the wording to refer to "dark background color" (or generally "generated dark color"), not border color.

Suggested change
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark border color has enough contrast with text color in dark mode
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark background color has enough contrast with text color in dark mode

Copilot uses AI. Check for mistakes.
* @param isBackground True to set background color, false to set text color
* @param isDarkMode Whether element is in dark mode now
* @param darkColorHandler @optional The dark color handler object to help manager dark mode color
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark border color has enough contrast with text color in dark mode
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

JSDoc for setColor's new comparingColor param mentions "generated dark border color", but this param is passed when generating dark background colors (to keep contrast with text). Please correct the wording so it matches the actual behavior.

Suggested change
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark border color has enough contrast with text color in dark mode
* @param comparingColor @optional When generating dark color for a background, we can provide the text color as comparingColor to make sure the generated dark background color has enough contrast with the text color in dark mode

Copilot uses AI. Check for mistakes.
Comment on lines 30 to 35
includeSelf: boolean,
direction: 'lightToDark' | 'darkToLight',
darkColorHandler?: DarkColorHandler,
transformColorOptions?: TransformColorOptions
transformColorOptions?: TransformColorOptions,
defaultTextColor?: string
) {
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

transformColor now has a new defaultTextColor parameter (used to propagate inherited text color for background contrast), but the exported function's documentation block above doesn't describe it. Please update the JSDoc to include this parameter and its effect on background color transformation.

Copilot uses AI. Check for mistakes.
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