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

feat: get and set body color #1357

Merged
merged 20 commits into from
Aug 27, 2024
Merged

feat: get and set body color #1357

merged 20 commits into from
Aug 27, 2024

Conversation

umutsoysalansys
Copy link
Contributor

@umutsoysalansys umutsoysalansys commented Aug 12, 2024

Description

Summary:
This pull request introduces new functionality to the geometry core, enabling users to get and set the color of a Body object. This enhancement extends the existing API by adding properties and methods that allow for seamless color management within the design environment.

New Features:
Get Color: Users can now retrieve the current color of a Body object using the color property. This property will return the color in a hexadecimal format (e.g., "#0000FF").

Set Color: The set_color method allows users to set the color of a Body object. The method accepts color values in various formats:

Formats supported:
Hexadecimal color codes (e.g., "#0000FF").
Named color strings (e.g., "blue").
RGB values as a tuple, either in the range (0-1) as floats or (0-255) as integers.

image.

Usage Example

# Create the sphere
sphere = design.create_sphere("particle", Point3D([0.0, 0.0, 0.0], unit=unit), Distance(0.5))

# Import matplotlib.colors
import matplotlib.colors as mcolors

# Create a blue color using RGBA and assign it to the sphere
blue_color = mcolors.to_rgba("#0000FF")
sphere.set_color(blue_color)

# Set color directly from a hex code
sphere.set_color("#0000FF")

# Set color directly using RGB integers (values between 0-255)
sphere.set_color((0, 255, 0))

# Set the color using a named color
sphere.set_color("green")

# Set color using RGB floats (values between 0.0-1.0)
sphere.set_color((1.0, 0.0, 0.0))

Issue linked

#433 related but also recent customer feedback.

Checklist

  • I have tested my changes locally.
  • I have added necessary documentation or updated existing documentation.
  • I have followed the coding style guidelines of this project.
  • I have added appropriate unit tests.
  • I have reviewed my changes before submitting this pull request.
  • I have linked the issue or issues that are solved to the PR if any.
  • I have assigned this PR to myself.
  • I have added the minimum version decorator to any new backend method implemented.
  • I have made sure that the title of my PR follows Conventional commits style (e.g. feat: extrude circle to cylinder)

@umutsoysalansys umutsoysalansys self-assigned this Aug 12, 2024
@umutsoysalansys umutsoysalansys changed the title get and set for colors feat: get and set for colors Aug 12, 2024
@umutsoysalansys umutsoysalansys changed the title feat: get and set for colors feat: get and set body color Aug 12, 2024
@umutsoysalansys umutsoysalansys marked this pull request as ready for review August 12, 2024 21:47
@umutsoysalansys umutsoysalansys requested a review from a team as a code owner August 12, 2024 21:47
@RobPasMue
Copy link
Member

RobPasMue commented Aug 13, 2024

Hey @umutsoysalansys - you could use matplotlib for this purpose. See this example:

import matplotlib.colors as mcolors

# Convert RGB to HEX
rgb = (0.5, 0.5, 0.5)  # values between 0 and 1
hex_code = mcolors.to_hex(rgb)
print(hex_code)  # Output: '#808080'

# Convert color name to HEX
color_name = 'blue'
hex_code = mcolors.to_hex(mcolors.CSS4_COLORS[color_name])
print(hex_code)  # Output: '#0000FF'

# Convert HEX to HEX (just returns the same HEX code)
hex_code = '#ff6347'
hex_code = mcolors.to_hex(hex_code)
print(hex_code)  # Output: '#ff6347'

Matplotlib is already a dependency so no need to worry. You would need to implement some logic to check whether the input value exists in mcolors.CSS4_COLORS (to see if the user passed in a color name). You would also need to check whether the user is passing an RGB tuple or an RGBA tuple (which is what matplotlib accepts). Any RGB tuple (i.e. from 0 - 255 and always integers) should be transformed to RGBA. You can use something like this:

import matplotlib.colors as mcolors

def rgb_to_hex(rgb):
    # Normalize RGB values (0-255) to (0-1)
    rgb_normalized = [x / 255.0 for x in rgb]
    # Convert to hex (implicitly treats as RGBA with A=1)
    return mcolors.to_hex(rgb_normalized)

# Example usage
rgb = (127, 127, 127)  # RGB values in range 0-255
hex_code = rgb_to_hex(rgb)
print(hex_code)  # Output: '#7f7f7f'

Implement as many sanity checks as needed for this.

@github-actions github-actions bot added the enhancement New features or code improvements label Aug 13, 2024
@RobPasMue
Copy link
Member

Since unstable image is working, I am pushing image to stable

Copy link

codecov bot commented Aug 13, 2024

Codecov Report

Attention: Patch coverage is 83.67347% with 8 lines in your changes missing coverage. Please review.

Project coverage is 91.86%. Comparing base (a47c27b) to head (78a2965).
Report is 1 commits behind head on main.

Files Patch % Lines
src/ansys/geometry/core/designer/body.py 83.67% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1357      +/-   ##
==========================================
- Coverage   91.92%   91.86%   -0.06%     
==========================================
  Files          86       86              
  Lines        6739     6788      +49     
==========================================
+ Hits         6195     6236      +41     
- Misses        544      552       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@RobPasMue RobPasMue left a comment

Choose a reason for hiding this comment

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

LGTM!

@RobPasMue RobPasMue merged commit 0c1f9b8 into main Aug 27, 2024
43 checks passed
@RobPasMue RobPasMue deleted the feat/set-color branch August 27, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features or code improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants