Back to Python
2026-01-276 min read

Neumorphism Generator (Python Programming)

Learn Neumorphism Generator (Python Programming) step by step with clear examples and exercises.

Why This Matters

Neumorphism is a design trend that combines flat and skeuomorphic styles, creating a sense of depth and realism in digital interfaces. In this tutorial, we will create a Python script to generate neumorphic UI elements such as buttons, icons, and other shapes. This skill can be useful for web development, mobile app design, and UI/UX projects.

Neumorphism is becoming increasingly popular due to its unique aesthetic and versatility. By learning how to generate neumorphic elements programmatically, you'll be able to create custom designs quickly and efficiently, saving time and resources compared to manual design or using pre-made libraries. Additionally, understanding the underlying principles of neumorphism can help you make informed decisions about when and where to use this design trend in your projects.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Python programming concepts such as variables, functions, loops, and conditional statements. Familiarity with the matplotlib library for creating graphics is also helpful but not required, as we will cover the necessary parts in this lesson.

Core Concept

The core concept behind our Neumorphism Generator script involves calculating and applying visual effects to create a sense of depth and realism. We'll use simple mathematical formulas to generate shadows, highlights, and gradients for various shapes like circles, squares, and rectangles. The final result will be a neumorphic UI element that can be easily customized by modifying the input parameters.

Shadows and Highlights

To create the illusion of depth, we'll calculate shadows and highlights based on the shape's dimensions and position. For example, a square with a top-left shadow would have the following properties:

  1. Offset: The distance between the shadow and the actual shape (x, y).
  2. Blur Radius: The amount of blur applied to the shadow.
  3. Opacity: The transparency level of the shadow.
  4. Color: The color of the shadow.

We'll use these properties to generate shadows for different shapes and orientations. Similarly, highlights can be calculated by adjusting the offset, blur radius, opacity, and color to create a bright, glossy effect.

Gradients

Gradients are essential for creating a smooth transition between colors in neumorphic designs. We'll use linear gradients for our UI elements, which consist of two colors blending together at a specific angle. The gradient angle can be adjusted to create different visual effects, such as a horizontal gradient for buttons or a diagonal gradient for icons.

Gradient Angles and Directions

When creating neumorphic designs with gradients, it's essential to consider the gradient angle relative to the shape's orientation. For example:

  • A horizontal gradient (90 degrees) is ideal for buttons oriented vertically or horizontally.
  • A diagonal gradient (45 degrees) works well for icons or shapes with a slanted appearance.
  • A vertical gradient (0 or 180 degrees) can be used for tall, thin shapes like progress bars or sliders.

Worked Example

In this worked example, we'll create a neumorphic button using the core concepts discussed earlier. Our button will have a square shape with a top-left shadow and a linear gradient fill.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

Define the button properties

width = 100

height = 50

x = 200

y = 300

shadow_offset_x = 5

shadow_offset_y = 5

shadow_blur_radius = 10

shadow_opacity = 0.5

shadow_color = (0, 0, 0)

gradient_colors = ('#FF6347', '#FF8E53')

gradient_angle = 90

Create the button shape

button = Rectangle((x, y), width, height, fill=False, edgecolor='none')

Calculate and apply the shadow

shadow = Rectangle((x + shadow_offset_x, y + shadow_offset_y), width - 2 shadow_offset_x, height - 2 shadow_offset_y,

facecolor=shadow_color, alpha=shadow_opacity)

button.add_child(shadow)

Create the gradient fill

gradient = plt.Rectangle((x, y), width, height, fill=False, edgecolor='none')

gradient_line = plt.gca().add_artist(gradient)

gradient_line.set_visible(False)

gradient_patch = gradient_line.get_children()[0]

gradient_patch.set_fc((gradient_colors[0][0], gradient_colors[0][1], gradient_colors[0][2], 0))

gradient_patch.set_ec((gradient_colors[1][0], gradient_colors[1][1], gradient_colors[1][2], 0))

gradient_patch.set_alpha(0.5)

gradient_patch.set_transform(button.get_transform())

Plot the button and show the figure

plt.figure()

plt.gca().add_artist(button)

plt.show()


This script generates a neumorphic button with a square shape, top-left shadow, and linear gradient fill. You can customize the properties to create different variations of the button.

Common Mistakes

  1. Forgetting to set the fill=False and edgecolor='none' properties for the button and gradient shapes. This will result in a solid-colored button without any neumorphic effects.
  2. Not adjusting the gradient angle properly. A horizontal gradient for a vertical button or vice versa can create an unnatural visual effect.
  3. Using incorrect shadow properties, such as a large blur radius or low opacity, which may make the shadow too prominent or invisible.
  4. Failing to set the alpha property of the gradient patch to make it partially transparent and blend with the background.
  5. Not setting the transform property for the gradient patch to match the button's transform matrix, resulting in a misaligned gradient fill.
  6. Additional Common Mistake: Forgetting to handle edge cases, such as when the shadow or highlight extends beyond the shape's boundaries. This can be addressed by checking the position of the shadow or highlight and adjusting its properties accordingly.

Practice Questions

  1. Modify the worked example to create a neumorphic icon with a circular shape and a diagonal gradient.
  2. Implement a function that generates a rectangular shadow with a custom offset, blur radius, opacity, and color.
  3. Create a neumorphic menu using multiple buttons with different shapes, gradients, and shadows.
  4. Add hover effects to the neumorphic button by changing its size or adding a subtle glow when the mouse pointer hovers over it.
  5. Implement a function that generates a neumorphic shape based on user-defined parameters such as width, height, gradient colors, and shadow properties.
  6. Additional Practice Question: Create a neumorphic progress bar with a filled and unfilled section, using linear gradients for the fill and an outline for the unfilled section.
  7. Additional Practice Question: Implement a function that generates a customizable neumorphic card, including a title, subtitle, and description, along with a shadow and gradient.

FAQ

What libraries are required to create neumorphic designs in Python?

  • Matplotlib is the primary library used for creating graphics and shapes. Other libraries like NumPy and Pillow can also be useful for image manipulation.

Can I use other programming languages to generate neumorphic UI elements?

  • Yes, you can create neumorphic designs using various programming languages such as JavaScript (with libraries like React or Three.js), Swift (for iOS app development), and Java (for Android app development).

How do I make my neumorphic designs responsive for different screen sizes?

  • To ensure your designs are responsive, you can use media queries in CSS to adjust the dimensions and properties of the UI elements based on the screen size. Alternatively, you can create multiple versions of your designs for different screen resolutions.

How do I add interactive elements like hover effects or click events to my neumorphic designs?

  • For web development, you can use JavaScript (with libraries like jQuery) to add interactivity to your designs by manipulating the DOM and handling user input events such as mouseover and click. For desktop applications, you can use event-driven programming concepts to create similar functionality.

Are there any pre-made neumorphic UI libraries available for Python?

  • Yes, there are several pre-made neumorphic UI libraries for Python, such as NeuPy, NukeUI, and PyNeumorphism. These libraries provide a simple way to create neumorphic designs without having to write complex code from scratch.
Neumorphism Generator (Python Programming) | Python | XQA Learn