When it comes to designing impressive User Interfaces (UI), colors matter. One essential color mode sophisticatedly used in Flutter is the "HSL Color". HSL (Hue, Saturation, and Lightness) is a popular method of representing colors and it opens up expressive and creative options when designing UI for your Flutter apps.
HSL (Hue, Saturation, Lightness) is a color space that describes colors (hue) and their intensity (saturation, lightness). RGB color format, which directly correlates to your display's red green, and blue channels, is another prevalent color format. However, HSL is more intuitive as it represents colors as we perceive and describe them.
In Flutter, HSL color is accessed via the HSLColor class. Unlike direct RGB values, HSL lets you select color palettes with shared hues or saturation in a more manageable way. It's particularly handy for creating color palettes where you need various shades of a specific color.
1 //Creating a green color object using HSL color 2 HSLColor hslColor = new HSLColor.fromAHSL(1.0, 120.0, 1.0, 0.5); 3 Color newColor = hslColor.toColor(); 4
This dedicated HSLColor class in Flutter harnesses the power of the HSL color space. It's defined with three properties - hue, saturation, and lightness.
Hue represented as an angle between [0°, 360°], gives us the actual color information. Saturation (0% to 100%) represents the color intensity - desaturated (0%) color becomes a shade of gray, while a fully saturated color (100%) denotes its full intensity. Lightness also ranges from 0% to 100%, where extremes represent black (0%) and white (100%).
Now, you may ask how about opacity? That's where the alpha channel comes in. The alpha parameter, ranging from 0 (fully transparent) to 1 (fully opaque), modifies the opacity of HSL colors.
1 //Representing the color orange in HSL format 2 HSLColor orange = HSLColor.fromAHSL(1.0, 30.0, 1.0, 0.5); 3
By embracing the HSL color space, Flutter HSL Color allows you to design UIs that are cohesive, accessible, and visually pleasing. You can create diverse color palettes, and adjust hue, saturation, and lightness values per your need and aesthetic choices.
Here's a scenario: Consider a shopping app with an 'image-to-color palette' feature, which extracts the colors from a product image and produces a matching color palette. Leveraging HSL in your Flutter project, you can manipulate colors to make consistent UI changes, create dynamic themes, adjust saturation, and more.
1 // Example of a function component that creates an HSL color 2 Widget buildColorCard(int h, double s) { 3 return Container( 4 color: HSLColor.fromAHSL(1.0, h.toDouble(), s, 0.5).toColor(), 5 width: 50, 6 height: 50, 7 ); 8 } 9
The Flutter color code picker is an advanced feature, and it allows you to pick colors from a coded palette. It's a programmer-friendly feature as it's flexible and allows you to use RGB, hex, and HSL color formats.
1 // Example of a code snippet with Flutter Color Code Picker 2 Picker( 3 pickerAreaPadding: const EdgeInsets.all(2.5), 4 enableAlpha: true, 5 colorPickerWidth: 300.0, 6 pickerColor: currentColor, 7 onColorChanged: changeColor, 8 showLabel: true, 9 paletteType: PaletteType.hsv, 10 pickerAreaBorderRadius: const BorderRadius.only( 11 topLeft: const Radius.circular(2.0), 12 topRight: const Radius.circular(2.0), 13 ), 14 ) 15
Through the course of this blog, we delved deep into the exceptional qualities of Flutter's HSL color system. Covering key topics from the basics of the HSL color class to creating a color object and storming through Flutter's sophisticated HSL color picker and color code picker, you are now equipped to take your app color palettes to a new level.
We journeyed through hue, saturation, lightness, and how to mutate these values to get the specific color our hearts desire. Flutter's comprehensive handling of HSL color truly amplifies your ability to create expressive, vibrant, and accessible UIs.
Time to put this knowledge into action. Hop onto your Flutter code editor and start playing around with these concepts. Try a dynamic theme change using HSL color picker or build a color palette. Have fun, experiment, and create impressive UIs. Don't forget to share your creations with us!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.