Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use @ant-design/fast-color instead #4070

Merged
merged 5 commits into from
Aug 7, 2024
Merged

refactor: use @ant-design/fast-color instead #4070

merged 5 commits into from
Aug 7, 2024

Conversation

likui628
Copy link
Collaborator

@likui628 likui628 commented Aug 7, 2024

Description

FastColor 支持的带deg的hsl

new FastColor('#666'); // short hex
new FastColor('#66ccff'); // hex
new FastColor('#66ccffaa'); // hex with alpha
new FastColor('rgba(102, 204, 255, .5)'); // old css rgb syntax
new FastColor('rgb(102 204 255 / .5)'); // new css rgb syntax
new FastColor('hsl(270, 60, 40, .5)'); // old css hsl syntax, with or without unit
new FastColor('hsl(270deg 60% 40% / 50%)'); // new css hsl syntax, with or without unit
new FastColor({ r: 102, g: 204, b: 255, a: 0.5 }); // rgb object
new FastColor({ h: 270, s: 0.6, l: 0.4, a: 0.5 }); // hsl object
new FastColor({ h: 270, s: 0.6, v: 0.4, a: 0.5 }); // hsv object

Unpacked Size

@ctrl/tinycolor 244 kB
@ant-design/fast-color 43.9 kB

isValidColor移除

FastColor("not-a-color") -> #000000
所以通过color转换的一定是合法的颜色值

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Checklist

ℹ️ Check all checkboxes - this will indicate that you have done everything in accordance with the rules in CONTRIBUTING.

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs:dev command.
  • Run the tests with pnpm test.
  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced new color conversion functions, convertToHsl, convertToHslCssVar, and convertToRgb, enhancing the color processing capabilities.
  • Improvements

    • Updated color handling by replacing the TinyColor library with FastColor, streamlining color conversions and improving performance.
    • Simplified badge color application logic by transitioning from validation to direct RGB conversion.
  • Bug Fixes

    • Enhanced color conversion tests for improved accuracy and reliability.
  • Refactor

    • Updated module exports to reflect changes in color handling, improving the organization of color-related logic across components.

Copy link

changeset-bot bot commented Aug 7, 2024

⚠️ No Changeset found

Latest commit: 8fb3971

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@likui628 likui628 changed the title refactor: Use @ant-design/fast-color instead refactor: use @ant-design/fast-color instead Aug 7, 2024
Copy link
Contributor

coderabbitai bot commented Aug 7, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes reflect a significant update in the color handling system of the project, transitioning from the TinyColor library to the FastColor library. This update enhances color conversion functionalities, introduces new methods for converting colors to RGB and HSL formats, and simplifies the codebase by removing obsolete components. Overall, it aims for a more efficient and maintainable approach to color management, improving clarity and consistency across various components.

Changes

Files Change Summary
packages/@core/base/shared/build.config.ts Updated source directory reference from src/colorful/index to src/color/index.
packages/@core/base/shared/package.json Updated dependency from @ctrl/tinycolor to @ant-design/fast-color version ^2.0.5.
packages/@core/base/shared/src/color/*.ts Replaced TinyColor with FastColor, added functions for HSL and RGB conversion, and improved color conversion tests.
packages/@core/ui-kit/shadcn-ui/src/components/menu-badge/menu-badge.vue Modified color handling to use convertToRgb, removing the previous validation step for badge colors.
packages/utils/src/index.ts Changed export from @vben-core/shared/colorful to @vben-core/shared/color.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Theme
    participant ColorProcessor

    User->>Theme: Change Theme Color
    Theme->>ColorProcessor: Process New Color
    ColorProcessor->>ColorProcessor: Convert to RGB
    ColorProcessor-->>Theme: Return Processed Color
    Theme-->>User: Update UI with New Color
Loading

🐰 In the meadow, colors gleam,
Fast and bright, a vibrant dream.
From Tiny to Fast, a leap we take,
With hues so bold, our hearts awake.
🎨 Embrace the change, let colors flow,
A rainbow's dance, in every glow! 🌈


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 861f39b and cb9bc55.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (6)
  • packages/@core/base/shared/package.json (1 hunks)
  • packages/@core/base/shared/src/colorful/convert.test.ts (2 hunks)
  • packages/@core/base/shared/src/colorful/convert.ts (3 hunks)
  • packages/@core/base/shared/src/colorful/generator.ts (2 hunks)
  • packages/effects/hooks/src/use-design-tokens.ts (4 hunks)
  • packages/effects/layouts/src/widgets/preferences/blocks/theme/builtin.vue (2 hunks)
Additional comments not posted (17)
packages/@core/base/shared/src/colorful/generator.ts (2)

3-3: Update import statement.

The import statement now includes Color and convertToHslCssVar from the convert module. Ensure that the convertToHslCssVar function is correctly implemented in the convert module.


16-16: Update color handling logic.

The TinyColor instance has been replaced with a Color instance. Ensure that the Color class provides the same functionality as TinyColor.

packages/@core/base/shared/src/colorful/convert.ts (5)

1-4: Correctly alias FastColor to Color.

The alias Color is correctly created for FastColor. Ensure that FastColor is correctly imported and used throughout the file.


25-29: Ensure toHsl method correctness.

The Color class should provide a toHsl method that returns an object with a, h, l, and s properties. Verify the correctness of this method.


47-47: Ensure isValid method correctness.

The Color class should provide an isValid method that returns a boolean indicating the validity of the color. Verify the correctness of this method.


10-14: Ensure toHsl method correctness.

The Color class should provide a toHsl method that returns an object with a, h, l, and s properties. Verify the correctness of this method.


34-35: Ensure toRgbString method correctness.

The Color class should provide a toRgbString method that returns a correctly formatted RGB string. Verify the correctness of this method.

packages/@core/base/shared/package.json (1)

58-58: Update dependency to @ant-design/fast-color.

The dependency on @ctrl/tinycolor has been replaced with @ant-design/fast-color. Ensure that the new dependency version ^2.0.5 is correct and compatible with the project.

packages/@core/base/shared/src/colorful/convert.test.ts (3)

3-8: LGTM! Import statement updated correctly.

The addition of convertToRgb to the import statement is appropriate and aligns with the new test cases.


35-39: LGTM! New test case for RGB conversion.

The test case for converting HSL to RGB CSS variable format is clear and correct.


41-45: LGTM! New test case for RGBA conversion.

The test case for converting HSLA to RGBA CSS variable format is clear and correct.

packages/effects/layouts/src/widgets/preferences/blocks/theme/builtin.vue (3)

12-12: LGTM! Import statement updated correctly.

The addition of Color and convertToHsl to the import statement is appropriate and aligns with the changes made to the file.


25-25: LGTM! Updated color handling logic.

The use of the Color class to convert themeColorPrimary to a hex string is appropriate and aligns with the new color handling logic.


29-29: LGTM! Simplified computed property.

The simplification of the builtinThemePresets computed property improves readability and maintainability.

packages/effects/hooks/src/use-design-tokens.ts (3)

4-4: LGTM! Import statement updated correctly.

The addition of convertToRgb to the import statement is appropriate and aligns with the changes made to the file.


105-105: LGTM! Updated color conversion logic.

The use of convertToRgb to convert HSL values to RGB is appropriate and aligns with the new color handling logic.


161-161: LGTM! Updated color conversion logic.

The use of convertToRgb to convert HSL values to RGB is appropriate and aligns with the new color handling logic.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cb9bc55 and 4986f0e.

Files selected for processing (2)
  • packages/@core/base/shared/src/colorful/convert.test.ts (3 hunks)
  • packages/@core/base/shared/src/colorful/convert.ts (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/@core/base/shared/src/colorful/convert.test.ts
  • packages/@core/base/shared/src/colorful/convert.ts

@likui628 likui628 marked this pull request as draft August 7, 2024 11:28
All FastColor objects are valid. So isValid is always true.
FastColor("not-a-color") -> `#000000`
@likui628 likui628 marked this pull request as ready for review August 7, 2024 12:25
@likui628 likui628 added dependencies Pull requests that update a dependency file chore and removed chore labels Aug 7, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4986f0e and 8fb3971.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (8)
  • packages/@core/base/shared/build.config.ts (1 hunks)
  • packages/@core/base/shared/package.json (2 hunks)
  • packages/@core/base/shared/src/color/convert.test.ts (2 hunks)
  • packages/@core/base/shared/src/color/convert.ts (1 hunks)
  • packages/@core/base/shared/src/color/generator.ts (2 hunks)
  • packages/@core/base/shared/src/index.ts (1 hunks)
  • packages/@core/ui-kit/shadcn-ui/src/components/menu-badge/menu-badge.vue (2 hunks)
  • packages/utils/src/index.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • packages/@core/base/shared/build.config.ts
  • packages/@core/base/shared/src/index.ts
Files skipped from review as they are similar to previous changes (1)
  • packages/@core/base/shared/package.json
Additional comments not posted (13)
packages/utils/src/index.ts (1)

2-2: Export change detected: Ensure compatibility.

The export statement has been updated from @vben-core/shared/colorful to @vben-core/shared/color. Ensure that all dependent modules or components are updated to use the new export.

Verification successful

Export change verified: No issues found.

The export statement has been updated from @vben-core/shared/colorful to @vben-core/shared/color. No occurrences of the old export were found in the codebase, indicating that all dependent modules or components have been updated to use the new export.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usage of the old export to ensure compatibility with the new export.

# Test: Search for the old export usage. Expect: No occurrences of the old export.
rg --type ts -A 5 $'@vben-core/shared/colorful'

Length of output: 46


Script:

#!/bin/bash
# Description: Verify all usage of the old export to ensure compatibility with the new export.

# Test: Search for the old export usage across all file types. Expect: No occurrences of the old export.
rg '@vben-core/shared/colorful'

Length of output: 31

packages/@core/base/shared/src/color/generator.ts (2)

3-3: Import change detected: Ensure correct integration.

The import statement has been updated to use Color from ./convert instead of TinyColor from @ctrl/tinycolor. Ensure that the new class is correctly integrated and that all necessary methods are available.


16-16: Ensure correct color conversion.

The generatorColorVariables function now uses the Color class for color conversion. Verify that the toHexString method returns the expected hexadecimal representation of the color.

packages/@core/base/shared/src/color/convert.ts (4)

1-3: New Color class introduced: Ensure correct usage.

The Color class is introduced from @ant-design/fast-color. Ensure that the class is correctly used and that all necessary methods are available.


28-32: Ensure correct HSL CSS variable conversion.

The convertToHslCssVar function converts a color to an HSL CSS variable. Verify that the conversion logic is correct and that the resulting string is in the expected format.

Verification successful

Verified: The convertToHslCssVar function correctly converts a color to an HSL CSS variable string.

The function implementation correctly converts the input color to HSL values and formats the string as expected.

  • convertToHslCssVar function implementation found in packages/@core/base/shared/src/color/convert.ts:
    function convertToHslCssVar(color: string): string {
      const { a, h, l, s } = new Color(color).toHsl();
      const hsl = `${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%`;
      return a < 1 ? `${hsl} / ${a}` : hsl;
    }
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the `convertToHslCssVar` function logic.

# Test: Search for the `convertToHslCssVar` function implementation. Expect: Correct conversion logic.
ast-grep --lang typescript --pattern $'function convertToHslCssVar($_) {
  $$$
}'

Length of output: 82


Script:

#!/bin/bash
# Description: Verify the `convertToHslCssVar` function logic.

# Test: Search for the `convertToHslCssVar` function implementation in the specified file.
rg 'function convertToHslCssVar' packages/@core/base/shared/src/color/convert.ts -A 5

Length of output: 316


13-17: Ensure correct HSL conversion.

The convertToHsl function converts a color to HSL format. Verify that the conversion logic is correct and that the resulting string is in the expected format.

Verification successful

Verified: Correct HSL conversion.

The convertToHsl function correctly converts a color to HSL format and returns the string in the expected format.

  • The function uses the Color class to convert the input color to its HSL components.
  • The HSL values are rounded and formatted into a string.
  • If the alpha value is less than 1, it is appended to the HSL string.

The resulting string format is hsl(h s% l%) or hsl(h s% l%) a.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the `convertToHsl` function logic.

# Test: Search for the `convertToHsl` function implementation. Expect: Correct conversion logic.
ast-grep --lang typescript --pattern $'function convertToHsl($_) {
  $$$
}'

Length of output: 76


Script:

#!/bin/bash
# Search for the `convertToHsl` function definition across the codebase.
rg 'function convertToHsl'

Length of output: 224


Script:

#!/bin/bash
# Extract the implementation of the `convertToHsl` function from the specified file.
ast-grep --lang typescript --pattern $'function convertToHsl($_) {
  $$$
}' packages/@core/base/shared/src/color/convert.ts

Length of output: 124


Script:

#!/bin/bash
# Search for the `convertToHsl` function in the specified file and include surrounding lines to capture the full implementation.
rg -A 10 'function convertToHsl' packages/@core/base/shared/src/color/convert.ts

Length of output: 783


34-36: Ensure correct RGB conversion.

The convertToRgb function converts a color to RGB format. Verify that the conversion logic is correct and that the resulting string is in the expected format.

packages/@core/ui-kit/shadcn-ui/src/components/menu-badge/menu-badge.vue (3)

6-6: Approved: Import statement for convertToRgb.

The import statement for convertToRgb aligns with the shift from color validation to color conversion.


39-39: Approved: Return statement in badgeStyle computed property.

The return statement now ensures that backgroundColor is always set to a valid RGB value, improving the reliability of the color application.


37-39: Approved: Direct conversion to RGB format.

The change to use convertToRgb for direct conversion to RGB format simplifies the logic and aligns with the new library's capabilities.

However, ensure that badgeClass.value always contains a valid color value to avoid any potential issues.

packages/@core/base/shared/src/color/convert.test.ts (3)

3-3: Approved: Import statement for convertToRgb.

The import statement for convertToRgb aligns with the shift in focus towards testing RGB conversions.


30-34: Approved: New test case for converting HSL to RGB format.

The new test case validates the conversion of HSL colors to RGB format, ensuring the correctness of the convertToRgb function.


36-39: Approved: New test case for converting HSL with alpha transparency to RGBA format.

The new test case validates the conversion of HSL colors with alpha transparency to RGBA format, ensuring the correctness of the convertToRgb function.

@anncwb anncwb merged commit 1d38fb6 into main Aug 7, 2024
16 checks passed
@anncwb anncwb deleted the fast-color branch August 7, 2024 13:28
@github-actions github-actions bot locked and limited conversation to collaborators Sep 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants