CMaterialColors帮助您在Jetpack Compose项目中轻松使用MaterialColors
你可以选择下面的任意一种方式引入本库
- 将位于lib/src/main/java/com/funny/cmaterialcolors/CMaterialColors.kt 的文件拷贝到您的项目中,按需修改包名即可
- 在项目级别的
build.gradle
中添加Maven仓库maven { url "https://jitpack.io" }
并在模块级别的build.gradle
中添加依赖implementation 'com.github.FunnySaltyFish:CMaterialColors:1.0.21
如果你好奇为什么第一个版本号会是1.0.21,我可以告诉你,因为之前的所有版本提交到Jitpack.io都构建失败了!得益于互相抄袭的博主们,这个库硬是到这个版本号才构建成功
如果你对整个发布过程感兴趣,可以参阅:blog
import com.funnty.cmaterialcolors.MaterialColors
/*...*/
Surface(color = MaterialColors.Red200) {
Text(text = "FunnySaltyFish", modifier = Modifier.padding(4.dp),color = MaterialColors.PurpleA700)
/*像这样写颜色就好*/
}
对,就是这么简单!
这个库的所有颜色不是手打的,它是通过下面的代码生成的:
import re
import os
"""
generate kotlin colors
@copyright : FunnySaltyFish [github](https://github.com/FunnySaltyFish)
@date : 2021/06/14 22:28:48
"""
text = """
<color name="Red50">#fde0dc</color>
<color name="Red100">#f9bdbb</color>
...
"""
def generate_colors(xml:str):
""" generate kotlin code by using android resources xml file
@params xml source xml file
"""
pattern_color = re.compile(r"<color name=\"(.+)\">(.+)</color>")
find_colors = re.findall(pattern=pattern_color,string=xml) #find all colors by re module
result_code = ""
for (name,color) in find_colors:
name = name.replace("_","") #replace redundant symbol "_"
color = color.replace("#","0xff")
color = color.upper() #change the color into a stander format
result_code += f"@Stable\nval {name} = Color({color})\n" #generate the kotlin code
return result_code
if __name__ == "__main__":
code = generate_colors(text)
file_path = "./code/code.txt"
if not os.path.exists(file_path): #create folder if that dose not exist
os.makedirs(os.path.dirname(file_path))
with open(file_path,"w",encoding="utf-8") as f:
f.write(code)
你可以在这里查看此脚本完整代码
要查看所有受支持的颜色,你可以下载demo.apk
. 它使用反射罗列了该库包含的所有颜色。