Skip to content

Show colorized items#1

Merged
KommuSoft merged 4 commits into
masterfrom
colorhashshow
Jun 1, 2025
Merged

Show colorized items#1
KommuSoft merged 4 commits into
masterfrom
colorhashshow

Conversation

@KommuSoft
Copy link
Copy Markdown
Member

@KommuSoft KommuSoft commented Jun 1, 2025

Summary by CodeRabbit

  • New Features
    • Added functions to display hash-derived colors as colored text in the terminal, with improved color contrast for readability.
  • Refactor
    • Enhanced color generation for better distribution and visual distinction.
  • Chores
    • Updated the version number.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 1, 2025

Walkthrough

The version number in the package configuration file was incremented. In the main module, two new functions for colorized terminal output were added, the hashing logic was refactored for better color distribution, and helper functions for ANSI color codes and contrast were introduced. The module's export list was updated accordingly.

Changes

File(s) Change Summary
colorhash.cabal Updated package version from 0.1.0.0 to 0.2.0.
src/Data/Hashable/Color.hs Refactored hashing logic with bit scrambling; added ANSI escape sequence helpers, contrast color calculation, and exported new functions for colorized terminal output (rgbHashBgShow, rgbHashFgShow).

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Data.Hashable.Color

    User->>Data.Hashable.Color: rgbHash(a)
    Data.Hashable.Color->>Data.Hashable.Color: _rgbHash(a)
    Data.Hashable.Color-->>User: (r, g, b)

    User->>Data.Hashable.Color: rgbHashBgShow(a)
    Data.Hashable.Color->>Data.Hashable.Color: _rgbHash(a)
    Data.Hashable.Color->>Data.Hashable.Color: _altColor(r, g, b)
    Data.Hashable.Color->>Data.Hashable.Color: _ansiSeqBg(r, g, b), _ansiSeqFg(alt)
    Data.Hashable.Color-->>User: Colored string with background and contrasting foreground

    User->>Data.Hashable.Color: rgbHashFgShow(a)
    Data.Hashable.Color->>Data.Hashable.Color: _rgbHash(a)
    Data.Hashable.Color->>Data.Hashable.Color: _altColor(r, g, b)
    Data.Hashable.Color->>Data.Hashable.Color: _ansiSeqFg(r, g, b), _ansiSeqBg(alt)
    Data.Hashable.Color-->>User: Colored string with foreground and contrasting background
Loading

Poem

A hop and a skip, new colors appear,
With hashes now brighter, and contrast so clear!
Show off your values in terminal style,
With backgrounds and fore, they’ll dazzle awhile.
Version hops up, the rainbow is cast—
Rabbits in code, coloring fast!
🐇🌈


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2b1c0fe and 33aef82.

📒 Files selected for processing (1)
  • src/Data/Hashable/Color.hs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: Run the tests (lts-18)
  • GitHub Check: Run the tests (lts-17)
  • GitHub Check: Run the tests (lts-14)
  • GitHub Check: Build the Haskell software (lts-23)
  • GitHub Check: Build the Haskell software (lts-22)
  • GitHub Check: Build the Haskell software (lts-19)
  • GitHub Check: Build the Haskell software (lts-14)
  • GitHub Check: Build the Haddock documentation (lts-21)
  • GitHub Check: Build the Haddock documentation (lts-23)
  • GitHub Check: Build the Haddock documentation (lts-20)
  • GitHub Check: Build the Haddock documentation (lts-22)
  • GitHub Check: Build the Haddock documentation (lts-18)
  • GitHub Check: Build the Haddock documentation (lts-19)
  • GitHub Check: Build the Haddock documentation (lts-17)
  • GitHub Check: Build the Haddock documentation (lts-16)
  • GitHub Check: Build the Haddock documentation (lts-12)
  • GitHub Check: Build the Haddock documentation (lts-15)
  • GitHub Check: Build the Haddock documentation (lts-14)
  • GitHub Check: Build the Haddock documentation (lts-13)
  • GitHub Check: Code formatter
🔇 Additional comments (6)
src/Data/Hashable/Color.hs (6)

15-23: LGTM: Module exports properly updated.

The module exports have been correctly updated to include the new colorized show functions while maintaining the existing rgbHash function.


25-25: LGTM: Appropriate imports and type definition.

The Data.Bits import is correctly added for the new bit manipulation functions, and the Col8 type alias provides a clean abstraction for RGB tuples.

Also applies to: 31-31


36-45: LGTM: Well-implemented hash scrambling for better color distribution.

The _scramble function uses a good bit-mixing technique with XOR and multiplication by a prime-like constant. The double scrambling in _rgbHash should provide excellent color distribution by reducing clustering of similar hash values.


47-55: LGTM: Correct ANSI escape sequence generation.

The ANSI sequence functions properly implement 24-bit color codes using the correct format (\027[38;2;r;g;b;m for foreground and \027[48;2;r;g;b;m for background).


70-70: LGTM: Clean refactoring to use helper function.

The rgbHash function has been properly refactored to use the new _rgbHash helper, maintaining the same interface while benefiting from improved hash distribution.


72-94: LGTM: Well-designed colorized terminal output functions.

Both rgbHashBgShow and rgbHashFgShow are well-implemented:

  • Properly handle 24-bit color ANSI sequences
  • Use contrasting colors for readability (hash color for one channel, contrasting black/white for the other)
  • Include proper reset sequence (\027[0m) to avoid affecting subsequent terminal output
  • Comprehensive documentation explaining 24-bit color requirements

The design correctly inverts the foreground/background relationship between the two functions.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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
Copy Markdown

@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: 1

🧹 Nitpick comments (3)
src/Data/Hashable/Color.hs (3)

36-37: Add documentation for the hash mixing function.

The implementation looks correct for hash mixing, but consider adding a comment explaining the purpose of the scrambling and the choice of the mixing constant.

+-- | Mix bits of a hash value for better color distribution
 _scramble :: Int -> Int
 _scramble x = ((shiftR x 16) `xor` x) * 0x45d9f3b

39-45: Consider if double scrambling is necessary.

The implementation correctly extracts RGB components, but applying _scramble twice might be overkill. Consider testing if single scrambling provides sufficient color distribution.

If single scrambling proves sufficient:

-    h = _scramble (_scramble (hash x))
+    h = _scramble (hash x)

72-83: LGTM! Well-implemented colorized background function.

The function correctly applies ANSI codes and resets terminal state. Consider adding a usage example in the documentation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d438683 and 2b1c0fe.

📒 Files selected for processing (2)
  • colorhash.cabal (1 hunks)
  • src/Data/Hashable/Color.hs (2 hunks)
🔇 Additional comments (6)
colorhash.cabal (1)

4-4: LGTM! Appropriate version bump for new functionality.

The minor version increment correctly reflects the addition of new public functions while maintaining backward compatibility.

src/Data/Hashable/Color.hs (5)

15-23: LGTM! Clean module structure with well-organized exports.

The module exports are logically grouped and the new functions are appropriately categorized under "Show items with color".


25-31: LGTM! Appropriate imports and clean type alias.

The Data.Bits import is necessary for the hash scrambling operations, and the Col8 type alias improves code readability.


47-54: LGTM! Correct ANSI escape sequence implementation.

The 24-bit color ANSI codes are properly formatted and the function composition approach is clean and efficient.


68-70: LGTM! Good refactoring for code reuse.

Using the shared _rgbHash helper improves consistency and maintainability.


84-94: LGTM! Well-implemented colorized foreground function.

The function correctly applies ANSI codes and resets terminal state. The symmetric design with rgbHashBgShow is clean and intuitive.

Comment on lines +56 to +59
_altColor :: Col8 -> Col8
_altColor ~(r, g, b)
| 1063 * fromEnum r + 3576 * fromEnum g + 361 * fromEnum b <= 200000 = (255, 255, 255)
| otherwise = (0, 0, 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify the luminance calculation weights.

The contrast calculation uses non-standard luminance weights. Consider using ITU-R BT.709 standard weights for more accurate perceptual luminance.

The current weights (1063, 3576, 361) don't match standard RGB-to-luminance coefficients. Standard weights are approximately (2990, 5870, 1140) when scaled to avoid floating-point arithmetic. Please verify if these custom weights are intentional or if they should be updated to standard values.


🌐 Web query:

What are the standard ITU-R BT.709 RGB to luminance conversion coefficients?

💡 Result:

ITU-R BT.709 defines specific coefficients for converting RGB values to luminance (Y'). According to the standard, the RGB to luminance conversion coefficients are:

  • Red (R): 0.2126
  • Green (G): 0.7152
  • Blue (B): 0.0722[4][7][8]

These coefficients are used in the following formula to derive the luminance signal:

Y' = 0.2126 × R' + 0.7152 × G' + 0.0722 × B'[4][5]

Where R', G', and B' represent the gamma-corrected (non-linear) RGB values.

The BT.709 coefficients differ from those used in other standards like Rec.601/BT.601, which uses 0.299 for red, 0.587 for green, and 0.114 for blue[7]. This difference is significant because the BT.709 coefficients are designed to match the primaries and white points of HDTV systems, allowing luma to correspond more closely to actual luminance[8].

For the constant luminance version (YC') specified in BT.2020, slightly different coefficients are used:

  • Red: 0.2627
  • Green: 0.6780
  • Blue: 0.0593[5]

The BT.709 standard is primarily used for HDTV applications and has become the basis for many modern video formats and color space conversions.

Citations:


Use ITU-R BT.709 Luminance Weights for Contrast

The _altColor function currently applies custom weights (1063, 3576, 361) which don’t align with BT.709’s perceptual luminance coefficients. Update to the standard values and adjust the threshold accordingly.

• File: src/Data/Hashable/Color.hs
Lines: 56–59

Suggested diff:

-altColor :: Col8 -> Col8
-_altColor ~(r, g, b)
-  | 1063 * fromEnum r + 3576 * fromEnum g + 361 * fromEnum b <= 200000 = (255, 255, 255)
-  | otherwise = (0, 0, 0)
+_altColor :: Col8 -> Col8
+_altColor ~(r, g, b)
+  -- BT.709 Y' = 0.2126R + 0.7152G + 0.0722B (scaled to integers)
+  | 2126 * fromEnum r + 7152 * fromEnum g +  722 * fromEnum b <= 400000 = (255, 255, 255)
+  | otherwise = (0,   0,   0)

• New threshold 400000 preserves the original 15.7% luminance cutoff (200 000/1 275 000 ≈ 0.157 → 0.157×2 550 000 ≈ 400 000).

This ensures more accurate, standard-compliant contrast decisions.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_altColor :: Col8 -> Col8
_altColor ~(r, g, b)
| 1063 * fromEnum r + 3576 * fromEnum g + 361 * fromEnum b <= 200000 = (255, 255, 255)
| otherwise = (0, 0, 0)
_altColor :: Col8 -> Col8
_altColor ~(r, g, b)
-- BT.709 Y' = 0.2126R + 0.7152G + 0.0722B (scaled to integers)
| 2126 * fromEnum r + 7152 * fromEnum g + 722 * fromEnum b <= 400000 = (255, 255, 255)
| otherwise = (0, 0, 0)
🤖 Prompt for AI Agents
In src/Data/Hashable/Color.hs around lines 56 to 59, the _altColor function uses
non-standard luminance weights (1063, 3576, 361) for contrast calculation.
Replace these with the ITU-R BT.709 standard weights scaled appropriately (2990
for red, 5870 for green, and 1140 for blue) and update the luminance threshold
to 400000 to maintain the original contrast cutoff. This change aligns the
luminance calculation with perceptual standards for better accuracy.

@KommuSoft KommuSoft merged commit 508ac62 into master Jun 1, 2025
42 checks passed
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.

1 participant