gen_sokol_color.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #-------------------------------------------------------------------------------
  2. # Generate the sokol_color.h header from a predefined palette
  3. #-------------------------------------------------------------------------------
  4. # LICENSE
  5. # =======
  6. #
  7. # zlib/libpng license
  8. #
  9. # Copyright (c) 2020 Stuart Adams
  10. #
  11. # This software is provided 'as-is', without any express or implied warranty.
  12. # In no event will the authors be held liable for any damages arising from the
  13. # use of this software.
  14. #
  15. # Permission is granted to anyone to use this software for any purpose,
  16. # including commercial applications, and to alter it and redistribute it
  17. # freely, subject to the following restrictions:
  18. #
  19. # 1. The origin of this software must not be misrepresented; you must not
  20. # claim that you wrote the original software. If you use this software in a
  21. # product, an acknowledgment in the product documentation would be
  22. # appreciated but is not required.
  23. #
  24. # 2. Altered source versions must be plainly marked as such, and must not
  25. # be misrepresented as being the original software.
  26. #
  27. # 3. This notice may not be removed or altered from any source
  28. # distribution.
  29. colors = [
  30. ("Alice Blue", 0xF0F8FFFF),
  31. ("Antique White", 0xFAEBD7FF),
  32. ("Aqua", 0x00FFFFFF),
  33. ("Aquamarine", 0x7FFFD4FF),
  34. ("Azure", 0xF0FFFFFF),
  35. ("Beige", 0xF5F5DCFF),
  36. ("Bisque", 0xFFE4C4FF),
  37. ("Black", 0x000000FF),
  38. ("Blanched Almond", 0xFFEBCDFF),
  39. ("Blue", 0x0000FFFF),
  40. ("Blue Violet", 0x8A2BE2FF),
  41. ("Brown", 0xA52A2AFF),
  42. ("Burlywood", 0xDEB887FF),
  43. ("Cadet Blue", 0x5F9EA0FF),
  44. ("Chartreuse", 0x7FFF00FF),
  45. ("Chocolate", 0xD2691EFF),
  46. ("Coral", 0xFF7F50FF),
  47. ("Cornflower Blue", 0x6495EDFF),
  48. ("Cornsilk", 0xFFF8DCFF),
  49. ("Crimson", 0xDC143CFF),
  50. ("Cyan", 0x00FFFFFF),
  51. ("Dark Blue", 0x00008BFF),
  52. ("Dark Cyan", 0x008B8BFF),
  53. ("Dark Goldenrod", 0xB8860BFF),
  54. ("Dark Gray", 0xA9A9A9FF),
  55. ("Dark Green", 0x006400FF),
  56. ("Dark Khaki", 0xBDB76BFF),
  57. ("Dark Magenta", 0x8B008BFF),
  58. ("Dark Olive Green", 0x556B2FFF),
  59. ("Dark Orange", 0xFF8C00FF),
  60. ("Dark Orchid", 0x9932CCFF),
  61. ("Dark Red", 0x8B0000FF),
  62. ("Dark Salmon", 0xE9967AFF),
  63. ("Dark Sea Green", 0x8FBC8FFF),
  64. ("Dark Slate Blue", 0x483D8BFF),
  65. ("Dark Slate Gray", 0x2F4F4FFF),
  66. ("Dark Turquoise", 0x00CED1FF),
  67. ("Dark Violet", 0x9400D3FF),
  68. ("Deep Pink", 0xFF1493FF),
  69. ("Deep Sky Blue", 0x00BFFFFF),
  70. ("Dim Gray", 0x696969FF),
  71. ("Dodger Blue", 0x1E90FFFF),
  72. ("Firebrick", 0xB22222FF),
  73. ("Floral White", 0xFFFAF0FF),
  74. ("Forest Green", 0x228B22FF),
  75. ("Fuchsia", 0xFF00FFFF),
  76. ("Gainsboro", 0xDCDCDCFF),
  77. ("Ghost White", 0xF8F8FFFF),
  78. ("Gold", 0xFFD700FF),
  79. ("Goldenrod", 0xDAA520FF),
  80. ("Gray", 0xBEBEBEFF),
  81. ("Web Gray", 0x808080FF),
  82. ("Green", 0x00FF00FF),
  83. ("Web Green", 0x008000FF),
  84. ("Green Yellow", 0xADFF2FFF),
  85. ("Honeydew", 0xF0FFF0FF),
  86. ("Hot Pink", 0xFF69B4FF),
  87. ("Indian Red", 0xCD5C5CFF),
  88. ("Indigo", 0x4B0082FF),
  89. ("Ivory", 0xFFFFF0FF),
  90. ("Khaki", 0xF0E68CFF),
  91. ("Lavender", 0xE6E6FAFF),
  92. ("Lavender Blush", 0xFFF0F5FF),
  93. ("Lawn Green", 0x7CFC00FF),
  94. ("Lemon Chiffon", 0xFFFACDFF),
  95. ("Light Blue", 0xADD8E6FF),
  96. ("Light Coral", 0xF08080FF),
  97. ("Light Cyan", 0xE0FFFFFF),
  98. ("Light Goldenrod", 0xFAFAD2FF),
  99. ("Light Gray", 0xD3D3D3FF),
  100. ("Light Green", 0x90EE90FF),
  101. ("Light Pink", 0xFFB6C1FF),
  102. ("Light Salmon", 0xFFA07AFF),
  103. ("Light Sea Green", 0x20B2AAFF),
  104. ("Light Sky Blue", 0x87CEFAFF),
  105. ("Light Slate Gray", 0x778899FF),
  106. ("Light Steel Blue", 0xB0C4DEFF),
  107. ("Light Yellow", 0xFFFFE0FF),
  108. ("Lime", 0x00FF00FF),
  109. ("Lime Green", 0x32CD32FF),
  110. ("Linen", 0xFAF0E6FF),
  111. ("Magenta", 0xFF00FFFF),
  112. ("Maroon", 0xB03060FF),
  113. ("Web Maroon", 0x800000FF),
  114. ("Medium Aquamarine", 0x66CDAAFF),
  115. ("Medium Blue", 0x0000CDFF),
  116. ("Medium Orchid", 0xBA55D3FF),
  117. ("Medium Purple", 0x9370DBFF),
  118. ("Medium Sea Green", 0x3CB371FF),
  119. ("Medium Slate Blue", 0x7B68EEFF),
  120. ("Medium Spring Green", 0x00FA9AFF),
  121. ("Medium Turquoise", 0x48D1CCFF),
  122. ("Medium Violet Red", 0xC71585FF),
  123. ("Midnight Blue", 0x191970FF),
  124. ("Mint Cream", 0xF5FFFAFF),
  125. ("Misty Rose", 0xFFE4E1FF),
  126. ("Moccasin", 0xFFE4B5FF),
  127. ("Navajo White", 0xFFDEADFF),
  128. ("Navy Blue", 0x000080FF),
  129. ("Old Lace", 0xFDF5E6FF),
  130. ("Olive", 0x808000FF),
  131. ("Olive Drab", 0x6B8E23FF),
  132. ("Orange", 0xFFA500FF),
  133. ("Orange Red", 0xFF4500FF),
  134. ("Orchid", 0xDA70D6FF),
  135. ("Pale Goldenrod", 0xEEE8AAFF),
  136. ("Pale Green", 0x98FB98FF),
  137. ("Pale Turquoise", 0xAFEEEEFF),
  138. ("Pale Violet Red", 0xDB7093FF),
  139. ("Papaya Whip", 0xFFEFD5FF),
  140. ("Peach Puff", 0xFFDAB9FF),
  141. ("Peru", 0xCD853FFF),
  142. ("Pink", 0xFFC0CBFF),
  143. ("Plum", 0xDDA0DDFF),
  144. ("Powder Blue", 0xB0E0E6FF),
  145. ("Purple", 0xA020F0FF),
  146. ("Web Purple", 0x800080FF),
  147. ("Rebecca Purple", 0x663399FF),
  148. ("Red", 0xFF0000FF),
  149. ("Rosy Brown", 0xBC8F8FFF),
  150. ("Royal Blue", 0x4169E1FF),
  151. ("Saddle Brown", 0x8B4513FF),
  152. ("Salmon", 0xFA8072FF),
  153. ("Sandy Brown", 0xF4A460FF),
  154. ("Sea Green", 0x2E8B57FF),
  155. ("Seashell", 0xFFF5EEFF),
  156. ("Sienna", 0xA0522DFF),
  157. ("Silver", 0xC0C0C0FF),
  158. ("Sky Blue", 0x87CEEBFF),
  159. ("Slate Blue", 0x6A5ACDFF),
  160. ("Slate Gray", 0x708090FF),
  161. ("Snow", 0xFFFAFAFF),
  162. ("Spring Green", 0x00FF7FFF),
  163. ("Steel Blue", 0x4682B4FF),
  164. ("Tan", 0xD2B48CFF),
  165. ("Teal", 0x008080FF),
  166. ("Thistle", 0xD8BFD8FF),
  167. ("Tomato", 0xFF6347FF),
  168. ("Transparent", 0x00000000),
  169. ("Turquoise", 0x40E0D0FF),
  170. ("Violet", 0xEE82EEFF),
  171. ("Wheat", 0xF5DEB3FF),
  172. ("White", 0xFFFFFFFF),
  173. ("White Smoke", 0xF5F5F5FF),
  174. ("Yellow", 0xFFFF00FF),
  175. ("Yellow Green", 0x9ACD32FF)
  176. ]
  177. header = open("sokol_color.h", "w")
  178. header.write("""#if defined(SOKOL_IMPL) && !defined(SOKOL_COLOR_IMPL)
  179. #define SOKOL_COLOR_IMPL
  180. #endif
  181. #ifndef SOKOL_COLOR_INCLUDED
  182. /*
  183. sokol_color.h -- sg_color utilities
  184. This header was generated by gen_sokol_color.py. Do not modify it.
  185. Project URL: https://github.com/floooh/sokol
  186. Include the following headers before including sokol_color.h:
  187. sokol_gfx.h
  188. FEATURE OVERVIEW
  189. ================
  190. sokol_color.h defines preset colors based on the X11 color names,
  191. alongside utility functions to create and modify sg_color objects.
  192. The predefined colors are based on the X11 color names:
  193. https://en.wikipedia.org/wiki/X11_color_names
  194. This palette is useful for prototyping - lots of programmers are familiar with
  195. these colours due to their use in X11, web development and XNA / MonoGame. They
  196. are also handy when you want to reference a familiar color, but don't want to
  197. write it out by hand.
  198. COLORS
  199. ======
  200. The palette is defined using static const (or constexpr if you are using a
  201. C++ compiler) objects. These objects use lowercase names:
  202. static SOKOL_COLOR_CONSTEXPR sg_color sg_red = SG_RED;
  203. static SOKOL_COLOR_CONSTEXPR sg_color sg_green = SG_GREEN;
  204. static SOKOL_COLOR_CONSTEXPR sg_color sg_blue = SG_BLUE;
  205. An sg_color preset object like sg_red can be used to initialize
  206. an sg_pass_action:
  207. sg_pass_action pass_action = {
  208. .colors[0] = { .action=SG_ACTION_CLEAR, .value = sg_red }
  209. };
  210. Initializing an object with static storage duration is more complicated
  211. because of C language rules. Technically, a static const is not a
  212. compile-time constant in C. To work around this, the palette is also
  213. defined as a series of brace-enclosed list macro definitions. These
  214. definitions use uppercase names:
  215. #define SG_RED { 1.0f, 0.0f, 0.0f, 1.0f }
  216. #define SG_GREEN { 0.0f, 1.0f, 0.0f, 1.0f }
  217. #define SG_BLUE { 0.0f, 0.0f, 1.0f, 1.0f }
  218. A preset macro like SG_RED can be used to initialize objects with static
  219. storage duration:
  220. static struct {
  221. sg_pass_action pass_action;
  222. } state = {
  223. .pass_action = {
  224. .colors[0] = { .action = SG_ACTION_CLEAR, .value = SG_RED }
  225. }
  226. };
  227. A second set of macro definitions exists for colors packed as 32 bit integer
  228. values. These definitions are also uppercase, but use the _RGBA32 suffix:
  229. #define SG_RED_RGBA32 0xFF0000FF
  230. #define SG_GREEN_RGBA32 0x00FF00FF
  231. #define SG_BLUE_RGBA32 0x0000FFFF
  232. This is useful if your code makes use of packed colors, as sokol_gl.h does for its
  233. internal vertex format:
  234. sgl_begin_triangles();
  235. sgl_v2f_c1i( 0.0f, 0.5f, SG_RED_RGBA32);
  236. sgl_v2f_c1i( 0.5f, -0.5f, SG_GREEN_RGBA32);
  237. sgl_v2f_c1i(-0.5f, -0.5f, SG_BLUE_RGBA32);
  238. sgl_end();
  239. UTILITY FUNCTIONS
  240. =================
  241. Utility functions for creating colours are provided:
  242. - sg_make_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  243. Create a sg_color object from separate R, G, B, A bytes.
  244. - sg_make_color_1i(uint32_t rgba)
  245. Create a sg_color object from RGBA bytes packed into a 32-bit unsigned integer.
  246. - sg_color_lerp(const sg_color* color_a, const sg_color* color_b, float amount)
  247. Linearly interpolate a color.
  248. - sg_color_lerp_precise(const sg_color* color_a, const sg_color* color_b, float amount)
  249. Linearly interpolate a color. Less efficient but more precise than sg_color_lerp.
  250. - sg_color_multiply(const sg_color* color, float scale)
  251. Multiply each color component by the scale factor.
  252. LICENSE
  253. =======
  254. zlib/libpng license
  255. Copyright (c) 2020 Stuart Adams
  256. This software is provided 'as-is', without any express or implied warranty.
  257. In no event will the authors be held liable for any damages arising from the
  258. use of this software.
  259. Permission is granted to anyone to use this software for any purpose,
  260. including commercial applications, and to alter it and redistribute it
  261. freely, subject to the following restrictions:
  262. 1. The origin of this software must not be misrepresented; you must not
  263. claim that you wrote the original software. If you use this software in a
  264. product, an acknowledgment in the product documentation would be
  265. appreciated but is not required.
  266. 2. Altered source versions must be plainly marked as such, and must not
  267. be misrepresented as being the original software.
  268. 3. This notice may not be removed or altered from any source
  269. distribution.
  270. */
  271. #define SOKOL_COLOR_INCLUDED (1)
  272. #if !defined(SOKOL_GFX_INCLUDED)
  273. #error "Please include sokol_gfx.h before sokol_color.h"
  274. #endif
  275. #if defined(SOKOL_API_DECL) && !defined(SOKOL_GL_API_DECL)
  276. #define SOKOL_COLOR_API_DECL SOKOL_API_DECL
  277. #endif
  278. #ifndef SOKOL_COLOR_API_DECL
  279. #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_COLOR_IMPL)
  280. #define SOKOL_COLOR_API_DECL __declspec(dllexport)
  281. #elif defined(_WIN32) && defined(SOKOL_DLL)
  282. #define SOKOL_COLOR_API_DECL __declspec(dllimport)
  283. #else
  284. #define SOKOL_COLOR_API_DECL extern
  285. #endif
  286. #endif
  287. #ifdef __cplusplus
  288. #define SOKOL_COLOR_CONSTEXPR constexpr
  289. extern "C" {
  290. #else
  291. #define SOKOL_COLOR_CONSTEXPR const
  292. #endif
  293. SOKOL_COLOR_API_DECL sg_color sg_make_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
  294. SOKOL_COLOR_API_DECL sg_color sg_make_color_1i(uint32_t rgba);
  295. SOKOL_COLOR_API_DECL sg_color sg_color_lerp(const sg_color* color_a, const sg_color* color_b, float amount);
  296. SOKOL_COLOR_API_DECL sg_color sg_color_lerp_precise(const sg_color* color_a, const sg_color* color_b, float amount);
  297. SOKOL_COLOR_API_DECL sg_color sg_color_multiply(const sg_color* color, float scale);
  298. """)
  299. def unpack_rgba(color):
  300. red = (color & 0xFF000000) >> 24
  301. green = (color & 0xFF0000) >> 16
  302. blue = (color & 0xFF00) >> 8
  303. alpha = (color & 0xFF)
  304. return (red, green, blue, alpha)
  305. def add_documentation(color):
  306. documentation = "/* {name} color {{ R:{r}, G:{g}, B:{b}, A:{a} }} */\n"
  307. rgba = unpack_rgba(color[1])
  308. header.write(documentation.format(
  309. name = color[0], r = rgba[0], g = rgba[1], b = rgba[2], a = rgba[3]))
  310. for color in colors:
  311. add_documentation(color)
  312. init_color = "SG_" + color[0].upper().replace(" ", "_")
  313. init_color_definition = "#define {name} {{ {r}f, {g}f, {b}f, {a}f }}\n"
  314. rgba = unpack_rgba(color[1])
  315. r = rgba[0] / 255
  316. g = rgba[1] / 255
  317. b = rgba[2] / 255
  318. a = rgba[3] / 255
  319. r_text = "{:.1f}".format(r) if r.is_integer() else "{:.9g}".format(r)
  320. g_text = "{:.1f}".format(g) if g.is_integer() else "{:.9g}".format(g)
  321. b_text = "{:.1f}".format(b) if b.is_integer() else "{:.9g}".format(b)
  322. a_text = "{:.1f}".format(a) if a.is_integer() else "{:.9g}".format(a)
  323. header.write(init_color_definition.format(
  324. name = init_color, r = r_text, g = g_text, b = b_text, a = a_text))
  325. header.write("\n")
  326. for color in colors:
  327. add_documentation(color)
  328. init_color = "sg_" + color[0].lower().replace(" ", "_")
  329. init_color_definition = "static SOKOL_COLOR_CONSTEXPR sg_color {name} = {init};\n"
  330. init_color_name = "SG_" + color[0].upper().replace(" ", "_")
  331. header.write(init_color_definition.format(name = init_color, init = init_color_name))
  332. header.write("\n")
  333. for color in colors:
  334. add_documentation(color)
  335. hex_color = "0x{0:08X}".format(color[1])
  336. packed_color = "SG_" + color[0].upper().replace(" ", "_") + "_RGBA32"
  337. packed_color_definition = "#define {name} {rgba}\n"
  338. header.write(packed_color_definition.format(name = packed_color, rgba = hex_color))
  339. header.write("""
  340. #ifdef __cplusplus
  341. } /* extern "C" */
  342. inline sg_color sg_make_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  343. return sg_make_color_4b(r, g, b, a);
  344. }
  345. inline sg_color sg_make_color(uint32_t rgba) {
  346. return sg_make_color_1i(rgba);
  347. }
  348. inline sg_color sg_color_lerp(const sg_color& color_a, const sg_color& color_b, float amount) {
  349. return sg_color_lerp(&color_a, &color_b, amount);
  350. }
  351. inline sg_color sg_color_lerp_precise(const sg_color& color_a, const sg_color& color_b, float amount) {
  352. return sg_color_lerp_precise(&color_a, &color_b, amount);
  353. }
  354. inline sg_color sg_color_multiply(const sg_color& color, float scale) {
  355. return sg_color_multiply(&color, scale);
  356. }
  357. #endif /* __cplusplus */
  358. #endif /* SOKOL_COLOR_INCLUDED */
  359. /*-- IMPLEMENTATION ----------------------------------------------------------*/
  360. #ifdef SOKOL_COLOR_IMPL
  361. #define SOKOL_COLOR_IMPL_INCLUDED (1)
  362. #ifndef SOKOL_API_IMPL
  363. #define SOKOL_API_IMPL
  364. #endif
  365. #ifndef SOKOL_ASSERT
  366. #include <assert.h>
  367. #define SOKOL_ASSERT(c) assert(c)
  368. #endif
  369. static inline float _sg_color_clamp(float v, float low, float high) {
  370. if (v < low) {
  371. return low;
  372. } else if (v > high) {
  373. return high;
  374. }
  375. return v;
  376. }
  377. static inline float _sg_color_lerp(float a, float b, float amount) {
  378. return a + (b - a) * amount;
  379. }
  380. static inline float _sg_color_lerp_precise(float a, float b, float amount) {
  381. return ((1.0f - amount) * a) + (b * amount);
  382. }
  383. SOKOL_API_IMPL sg_color sg_make_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  384. sg_color result;
  385. result.r = r / 255.0f;
  386. result.g = g / 255.0f;
  387. result.b = b / 255.0f;
  388. result.a = a / 255.0f;
  389. return result;
  390. }
  391. SOKOL_API_IMPL sg_color sg_make_color_1i(uint32_t rgba) {
  392. return sg_make_color_4b(
  393. (uint8_t)(rgba >> 24),
  394. (uint8_t)(rgba >> 16),
  395. (uint8_t)(rgba >> 8),
  396. (uint8_t)(rgba >> 0)
  397. );
  398. }
  399. SOKOL_API_IMPL sg_color sg_color_lerp(const sg_color* color_a, const sg_color* color_b, float amount) {
  400. SOKOL_ASSERT(color_a);
  401. SOKOL_ASSERT(color_b);
  402. amount = _sg_color_clamp(amount, 0.0f, 1.0f);
  403. sg_color result;
  404. result.r = _sg_color_lerp(color_a->r, color_b->r, amount);
  405. result.g = _sg_color_lerp(color_a->g, color_b->g, amount);
  406. result.b = _sg_color_lerp(color_a->b, color_b->b, amount);
  407. result.a = _sg_color_lerp(color_a->a, color_b->a, amount);
  408. return result;
  409. }
  410. SOKOL_API_IMPL sg_color sg_color_lerp_precise(const sg_color* color_a, const sg_color* color_b, float amount) {
  411. SOKOL_ASSERT(color_a);
  412. SOKOL_ASSERT(color_b);
  413. amount = _sg_color_clamp(amount, 0.0f, 1.0f);
  414. sg_color result;
  415. result.r = _sg_color_lerp_precise(color_a->r, color_b->r, amount);
  416. result.g = _sg_color_lerp_precise(color_a->g, color_b->g, amount);
  417. result.b = _sg_color_lerp_precise(color_a->b, color_b->b, amount);
  418. result.a = _sg_color_lerp_precise(color_a->a, color_b->a, amount);
  419. return result;
  420. }
  421. SOKOL_API_IMPL sg_color sg_color_multiply(const sg_color* color, float scale) {
  422. SOKOL_ASSERT(color);
  423. sg_color result;
  424. result.r = color->r * scale;
  425. result.g = color->g * scale;
  426. result.b = color->b * scale;
  427. result.a = color->a * scale;
  428. return result;
  429. }
  430. #endif /* SOKOL_COLOR_IMPL */
  431. """)