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

news2024/9/22 5:28:00

前言
本专栏是关于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 alpha
    pub 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 alpha
    pub 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是实时更新的,此区域的临时变量会一直被复位,你选择的颜色值一般会闪现,然后归零。

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2091130.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

笔记整理—内核!启动!

常规启动时,各镜像都在SD卡中的各种分区中,内核放在kernel分区,从SD卡到DDR的连接处(内核不需要进行重定位,直接从链接处启动)。uboot从sd卡分区读使用movi命令。 使用fastboot指令可以查看分区情况&#x…

【赵渝强老师】MongoDB的MMAPv1存储引擎

在MongoDB 3.2版本以前,MongoDB使用MMAPv1作为默认的存储引擎。在MMAPv1的存储引擎中,包含以下的组成部分: Database 每个Database由一个.ns名称空间文件及若干个数据文件组成。数据文件从0开始编号,依次为.0、.1、.2等。数据文件…

小心GitHub账号被盗

最近有小伙伴反馈在 GitHub 上解压了不明文件之后,GitHub 账号被盗了。 事情是这样的: 有小伙伴在 GitHub 某仓库的 issue 中正常和人讨论问题,有个人光速回复了一条消息,给了一个链接,让下载一个名为 fix.rar 的文件…

C++系列-STL容器之list

STL容器之list list容器的基本结构list容器的特点list容器的优点list容器的缺点 list容器的构造函数list容器的常用接口list大小及空否list访问list迭代器相关list增删查改push and popinsert其它 list赋值操作 list容器的基本结构 list容器的内部结构是双向循环链表&#xff…

Java笔试面试题AI答之面向对象(8)

文章目录 43. 解释Java接口隔离原则和单一原则如何理解 ?单一职责原则(Single Responsibility Principle, SRP)接口隔离原则(Interface Segregation Principle, ISP) 44. Java 有没有 goto? 如果有,一般用…

004.Python爬虫系列_web请求全过程剖析(重点)

我 的 个 人 主 页:👉👉 失心疯的个人主页 👈👈 入 门 教 程 推 荐 :👉👉 Python零基础入门教程合集 👈👈 虚 拟 环 境 搭 建 :👉&…

2024年8月31日历史上的今天大事件早读

1449年8月31日 明朝“土木之变” 1907年8月31日 英、法、俄三国协约形成 1914年8月31日 “骆派”京韵大鼓的创建者骆玉笙诞生 1916年8月31日 蔡锷东渡日本养病 1935年8月31日 美国通过《中立法案》 1937年8月31日 日本华北方面军成立 1941年8月31日 晋察冀边区完成民主大…

2024最新最全:国内外人工智能AI工具网站大全!

国内外人工智能AI工具网站大全(一键收藏,应有尽有) 摘要一、AI写作工具二、AI图像工具 2.1、常用AI图像工具2.2、AI图片插画生成2.3、AI图片背景移除2.4、AI图片无损调整2.5、AI图片优化修复2.6、AI图片物体抹除 三、AI音频工具四、AI视频工…

南京观海微电子----CMOS门电路(OD门、传输门、双向模拟开关、三态门)

【 1. MOS管】 MOS管:绝缘栅型场效应管。 【 2. CMOS电路】 当NMOS管和PMOS管成对出现在电路中,且二者在工作中互补,称为CMOS管(Complementary Metal-Oxide-Semiconductor)。 电路结构 拉电流 如下图所示,输入低电平&#xff…

王者荣耀 设置游戏头像 不用微信头像

我们在微信 我 选择 设置 在里面找到 个人信息与权限 如果找不到看看有木有一个叫隐私的选项 点击 进入之后 选择授权管理 找到王者荣耀 然后点击右侧的小箭头进入 点击下面的 解除授权 确认一下 解除授权 然后重新打开王者 选择微信登录 我们这里 选择新建昵称头像 选…

线性代数之线性方程组

目录 线性方程组 1. 解的个数 齐次线性方程组: 非齐次线性方程组: 2. 齐次线性方程组的解 3. 非齐次线性方程组的解 4. 使用 Python 和 NumPy 求解线性方程组 示例代码 齐次线性方程组 非齐次线性方程组 示例结果 齐次线性方程组 非齐次线性…

Unity获取SceneView尺寸

获取SceneView尺寸 var sceneView SceneView.lastActiveSceneView; var size new Vector2(sceneView.position.width,sceneView.position.height);

Elasticsearch学习(1)-mac系统安装elasticsearch基础

Elasticsearch基础 1. 传统数据库与elasticsarch2. 下载Elasticsearch7. 经过上述所有操作,就可以得到一个具体的连接可视化页面3. 安装kibana4. 其余知识点 elasticsearch是什么? Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能…

sql-labs靶场(41-50)

四十一 1.查看数据库名 ?id-1 union select 1,2,database()-- 2.查看表名 ?id-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schemadatabase()-- 3.查看user表列名 ?id-1 union select 1,group_concat(column_name),3 from…

SpringMVC处理流程介绍

SpringMVC请求处理流程 发起请求到前端控制器(DispatcherServlet)前端控制器请求HandlerMapping查找Handler(可以根据xml配置,注解进行查找) 对应Spring源码 //在类DispatcherServlet里面 protected void doDispatch(HttpServletRequest request, HttpServletResponse respon…

Leetcode102二叉树的层序遍历(java实现)

今天分享的题目是lee102题,题目的描述如下: 可能做到这道题的小伙伴写过其他关于二叉树的题目,但是一般是使用递归的方式做一个深度遍历,而层序遍历我们该如何做呢? 解题思路:使用一个队列来记录本层节点&a…

浅谈新能源汽车充电桩安装以及防范

摘要:随着国家对绿色环保的倡导,新能源电动汽车应运而生,它们采用清洁能源替代传统能源,有效避免了对自然环境的污染,并减少了资源消耗,实现了资源的高效利用。新能源电动汽车的普及降低了使用成本&#xf…

Rust Linux开发人员自比道路建设者和寻路者的区别

红帽公司(Red Hat)的长期直接渲染管理器(Direct Rendering Manager,DRM)子系统维护者大卫-艾尔里(David Airlie)撰写了一篇有趣的博文,将开发人员的类型与筑路工人、寻路者与酒店进行…

800G OSFP光模块发展概述

在快速发展的高速网络领域,800G OSFP光模块的演变象征着创新与进步。自诞生以来,800G OSFP光模块凭借哪些独特优势脱颖而出?本文将重点介绍800G OSFP光模块的发展路径。 800G OSFP光模块发展路径 路径一:EML 路由 800G DR8 OSF…

Python进阶07-高级语法

零、文章目录 Python进阶07-高级语法 1、with语句 (1)文件操作 文件使用完后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的 # 第一步:打开文件 f open(python.txt…