<Rust>egui学习之小部件(七):如何在窗口中添加颜色选择器colorpicker部件?

2024-08-31 10:20

本文主要是介绍<Rust>egui学习之小部件(七):如何在窗口中添加颜色选择器colorpicker部件?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言
本专栏是关于Rust的GUI库egui的部件讲解及应用实例分析,主要讲解egui的源代码、部件属性、如何应用。

环境配置
系统:windows
平台:visual studio code
语言:rust
库:egui、eframe

概述
本文是本专栏的第七篇博文,主要讲述颜色选择器部件colorpicker的使用。

事实上,类似于iced,egui都提供了示例程序,本专栏的博文都是建立在官方示例程序以及源代码的基础上,进行的实例讲解。
即,本专栏的文章并非只是简单的翻译egui的官方示例与文档,而是针对于官方代码进行的实际使用,会在官方的代码上进行修改,包括解决一些问题。

系列博客链接:
1、<Rust>egui学习之小部件(一):如何在窗口及部件显示中文字符?
2、<Rust>egui学习之小部件(二):如何在egui窗口中添加按钮button以及标签label部件?
3、<Rust>egui学习之小部件(三):如何为窗口UI元件设置布局(间隔、水平、垂直排列)?
4、<Rust>egui学习之小部件(四):如何在窗口中添加滑动条部件?
5、<Rust>egui学习之小部件(五):如何在窗口中添加图像部件?
6、<Rust>egui学习之小部件(六):如何在窗口中添加菜单栏部件?

部件属性

在egui中,也提供了颜色选择器这样的部件color_picker,且有多重颜色样式:

源代码中提供的可调用选项:

/// # Colors
impl Ui {/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown.pub fn color_edit_button_srgba(&mut self, srgba: &mut Color32) -> Response {color_picker::color_edit_button_srgba(self, srgba, color_picker::Alpha::BlendOrAdditive)}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown.pub fn color_edit_button_hsva(&mut self, hsva: &mut Hsva) -> Response {color_picker::color_edit_button_hsva(self, hsva, color_picker::Alpha::BlendOrAdditive)}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in `sRGB` space.pub fn color_edit_button_srgb(&mut self, srgb: &mut [u8; 3]) -> Response {color_picker::color_edit_button_srgb(self, srgb)}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in linear RGB space.pub fn color_edit_button_rgb(&mut self, rgb: &mut [f32; 3]) -> Response {color_picker::color_edit_button_rgb(self, rgb)}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in `sRGBA` space with premultiplied alphapub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {let mut color = Color32::from_rgba_premultiplied(srgba[0], srgba[1], srgba[2], srgba[3]);let response = self.color_edit_button_srgba(&mut color);*srgba = color.to_array();response}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in `sRGBA` space without premultiplied alpha./// If unsure, what "premultiplied alpha" is, then this is probably the function you want to use.pub fn color_edit_button_srgba_unmultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {let mut rgba = Rgba::from_srgba_unmultiplied(srgba[0], srgba[1], srgba[2], srgba[3]);let response =color_picker::color_edit_button_rgba(self, &mut rgba, color_picker::Alpha::OnlyBlend);*srgba = rgba.to_srgba_unmultiplied();response}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in linear RGBA space with premultiplied alphapub fn color_edit_button_rgba_premultiplied(&mut self, rgba_premul: &mut [f32; 4]) -> Response {let mut rgba = Rgba::from_rgba_premultiplied(rgba_premul[0],rgba_premul[1],rgba_premul[2],rgba_premul[3],);let response = color_picker::color_edit_button_rgba(self,&mut rgba,color_picker::Alpha::BlendOrAdditive,);*rgba_premul = rgba.to_array();response}/// Shows a button with the given color./// If the user clicks the button, a full color picker is shown./// The given color is in linear RGBA space without premultiplied alpha./// If unsure, what "premultiplied alpha" is, then this is probably the function you want to use.pub fn color_edit_button_rgba_unmultiplied(&mut self, rgba_unmul: &mut [f32; 4]) -> Response {let mut rgba = Rgba::from_rgba_unmultiplied(rgba_unmul[0],rgba_unmul[1],rgba_unmul[2],rgba_unmul[3],);let response =color_picker::color_edit_button_rgba(self, &mut rgba, color_picker::Alpha::OnlyBlend);*rgba_unmul = rgba.to_rgba_unmultiplied();response}
}

我们选择rgb格式来看一下:

ui.color_edit_button_rgb(&mut self.color1);

在这里插入图片描述
color_edit_button提供一个按钮,当我们点击此按钮时,就会弹出颜色选择器,上图就是rgb格式下的选择器样式。
再看看其他选择器样式:

hsva

ui.color_edit_button_hsva(&mut self.color_hsva);

在这里插入图片描述
就不一一列举了。

需要注意的是,调用color_picker函数中添加的参数变量,最好不要是临时变量,因为updat是实时更新的,此区域的临时变量会一直被复位,你选择的颜色值一般会闪现,然后归零。

本例中的颜色值变量,是在结构体中提前添加的,直接调用即可。
这样就可以获取实时选择的颜色值了。

看一下实例演示:
在这里插入图片描述

这篇关于<Rust>egui学习之小部件(七):如何在窗口中添加颜色选择器colorpicker部件?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123631

相关文章

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

rust 中的 EBNF简介举例

《rust中的EBNF简介举例》:本文主要介绍rust中的EBNF简介举例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 什么是 EBNF?2. 核心概念3. EBNF 语法符号详解4. 如何阅读 EBNF 规则5. 示例示例 1:简单的电子邮件地址

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen