Libraryless. Click here for Pure Java version (16988L/126K).
1 | !7 |
2 | |
3 | module ShowRandomColor > DynModule { |
4 | RGB color; |
5 | S myGuess, yourName, lastOverwrite; |
6 | bool voiceMode = true; // voice input & output |
7 | |
8 | transient double maxDiff = 0.25; // max difference to assume it's a similar color |
9 | transient JPanel colorPanel; |
10 | transient JTextField tfYourName; |
11 | transient JButton btnSave; |
12 | transient Pair<S, Double> closestColor; |
13 | transient bool hadBounds; |
14 | |
15 | enhanceFrame { |
16 | /*onInternalFrameDeactivated(f/JInternalFrame, r { |
17 | dm_hideYesPopupForComponent(btnSave) |
18 | });*/ |
19 | if (!hadBounds) { |
20 | growInternalFrameLeft(f, 250); // space to type |
21 | growInternalFrameSouth(f, 150); // space to see the color |
22 | } |
23 | internalFrameTitleMenuItem(f, "Talk!", rThread talk); |
24 | } |
25 | |
26 | visualize { |
27 | hadBounds = dm_getBounds() != null; |
28 | dm_showTalkingSmiley(); |
29 | if (color == null) setField(color := makeRandomColor()); |
30 | dm_onTopInput_q(voidfunc(S s) { |
31 | if (!licensed()) ret; |
32 | //if (!dm_isActiveModule(module())) ret with print("not active"); |
33 | if (!voiceMode) ret; |
34 | S _lastOverwrite = lastOverwrite; |
35 | setField(lastOverwrite := null); |
36 | JComponent popup = dm_generalMap_get('yesPopup); |
37 | if "no problem|stop listening|thanks" {} |
38 | else if (matchStart_vbar("hi|hello|hey|carol", s)) dm_say("Hello there"); |
39 | else if (matchEnd("your question", s)) dm_say("yes?"); // as to your question |
40 | else if (matchStart_vbar("talk|ask", s)) newColor(); |
41 | else if (containsPhrase_vbar(s, "colorblind|colour blind")) dm_say("No problem. I'm not judging."); |
42 | else if (containsWord(s, "forget") && containsWord(s, "all") && containsWord_vbar(s, "colors|colours")) { |
43 | // TODO: backup |
44 | dm_callModule(dm_colorNamesModule(), 'deleteAll); |
45 | dm_hideYesPopupForComponent(btnSave); |
46 | setField(yourName := ""); |
47 | dm_setTopInput(""); |
48 | dm_say("OK, starting over."); |
49 | } else if (containsWord(s, "list") && containsWord_vbar(s, "show|open")) { |
50 | dm_showModule(dm_colorNamesModule()); |
51 | dm_say("Here it is"); |
52 | } else if (containsWord(s, "reload")) dm_reloadModuleInBackground(); |
53 | else if (containsPhrase(s, "how many")) dm_say("You have taught me " + n2(toInt(dm_callModule(dm_colorNamesModule(), 'conceptCount)), "color")); |
54 | else if (match_vbar("accept|except|save|definitely|and if it is|is if it is right|is this|right|enter|submit|send|you got it|close enough|that it is|bingo|confirm|confirming|confirmed|i said yes|it is|surely|is this the man|guess my man|just|sieve|yesterday's", s) |
55 | || matchStart_vbar("yes|yeah|yep|yay|absolutely|correct|you can say that|chore|shore|shorts|sure|is yes|okay|ok|that's correct|you are right|you're right|you're correct|you just said it", s) |
56 | || matchEnd_vbar("that's right|got it|could say that|could say so|that's it|couldn't be anything else", s)) { |
57 | confirm(); |
58 | } else if "no|nope|undo|scratch that|scrap that|oops|oopsie" { |
59 | removeFromParent(popup); |
60 | if (_lastOverwrite != null) { |
61 | setField(yourName := _lastOverwrite); |
62 | dm_say(oneOf("oops", "oopsie", "sorry")); |
63 | } else |
64 | dm_say("What is it then?"); |
65 | } else if (matchStart_vbar("no idea", s)) { |
66 | dm_say("OK I'll move on."); |
67 | newColor(); |
68 | } else if (matchStart_vbar("new|next|another|hit me|again|do it again", dropPrefix_repeat_matchVBar("give me|a", s))) |
69 | newColor(); |
70 | else { |
71 | print("Got input: " + s); |
72 | |
73 | // "that is" is often heard as "what is" or "where is" |
74 | S stuff = "no|actually|maybe|I think|it's|that's|this is|it is|that is|that looks like|I guess|or something|I'd call that|I would call that|I would say|I'd call it|Some call it|Some people call it|is what it is|definitely|of course|well|rather|confirmed|called|that would be called|I say|I said|damn it|what is|make that|a|my friend|buddy|you silly thing|or|just|simply|straight|plain|normal|simple|nothing but|really|good old|plain old|what's|playing|pure|dude|my man|let's|sir|is this|confirmed|confirming|where is|is|because|obviously|there is|say"; |
75 | s = dropPrefix_repeat_matchVBar(stuff, s); |
76 | s = dropSuffix_repeat_matchVBar(stuff, s); |
77 | s = replaceWords(s, "blew", "blue", "great", "gray", "grey", "gray", "read", "red"); |
78 | print("Simplified to: " + s); |
79 | if (empty(s)) ret; |
80 | if (eqic(s, yourName)) ret with confirm(); |
81 | setField(lastOverwrite := yourName); |
82 | setField(yourName := s); |
83 | dm_showYesToAcceptPopupAbove(btnSave); |
84 | if (voiceMode) dm_say(s); |
85 | } |
86 | }); |
87 | JComponent theForm = vstackWithSpacing( |
88 | makeBold(fontSize(20, liveValueLabel(mapLiveValue(dm_fieldLiveValue('myGuess), S, func(S s) -> S { "I call this color: " + or2(s, "Uh... no idea") + "." })))), |
89 | westCenterAndEastWithMargin(fontSize(20, jBoldLabel("How do you call this color?")), |
90 | withSideMargin(15, 5, onEnter(tfYourName = focusOnShow(selectAllOnFocus( |
91 | fontSize(20, makeBold(dm_centeredFieldTextField('yourName))))), rThread saveName)), |
92 | btnSave = jBoldButton("SAVE", rThread saveName)), |
93 | withTopMargin(rightAlignedLine(jBoldButton("NEXT ONE PLEASE", rThread newColor)))); |
94 | |
95 | ret onFirstShow(rThread talk, |
96 | centerAndSouthWithMargin( |
97 | northAndCenterWithMargins( |
98 | westAndCenter(dm_fieldCheckBox('voiceMode), |
99 | rightAlignedLine( |
100 | withLabel("Color Code:", jBoldLiveValueLabel(mapLiveValue(func(O color) -> S { "#" + color }, S, dm_fieldLiveValue('color)))), |
101 | jPopDownButton_noText("Choose color...", rThread chooseColor))), |
102 | jCenteredSectionWithFont(sansSerifBold(18), " HERE'S A COLOR FOR YOU ", colorPanel = singleColorPanel(toColor(color)))), |
103 | withBottomMargin(withSideMargin(15, theForm)))); |
104 | } |
105 | |
106 | void newColor enter { |
107 | setColor(makeRandomColor()); |
108 | } |
109 | |
110 | void setColor(RGB color) enter { |
111 | setField(+color); |
112 | closestColor = (Pair) quickImport(dm_callModule(dm_colorNamesModule(), 'getClosestColor, color.getColor())); |
113 | S guess = pairA(closestColor); |
114 | bool closeEnough = guess != null && pairB(closestColor) <= maxDiff; |
115 | if (!closeEnough) guess = null; |
116 | setField(myGuess := or(guess, "no idea")); |
117 | setField(yourName := guess); |
118 | changeSingleColorPanel(colorPanel, color.getColor()); |
119 | selectAll(tfYourName); |
120 | if (guess != null) { |
121 | dm_showYesToAcceptPopupAbove(btnSave); |
122 | if (voiceMode) |
123 | dm_say("Is this " + myGuess + oneOf("", " that I see") + "?"); |
124 | } else if (voiceMode) |
125 | dm_say("Master! What is the name of this color?"); |
126 | } |
127 | |
128 | void saveName enter { |
129 | setField(yourName := trim(yourName)); |
130 | if (empty(yourName)) ret; |
131 | if (eqic(myGuess, yourName)) |
132 | dm_infoBoxOrTalk(voiceMode, randomLL( |
133 | "Hey I got this one right.", |
134 | "I'm good!", |
135 | "I'm so good.", |
136 | "I'm so smart.", |
137 | "I'm so smart, don't you think?", |
138 | "Yippieh!")); |
139 | else |
140 | dm_infoBoxOrTalk(voiceMode, quote(yourName) + " confirmed."); |
141 | dm_callModule(dm_colorNamesModule(), 'addColorName, color.getColor(), yourName, ""); |
142 | newColor(); |
143 | } |
144 | |
145 | RGB makeRandomColor() { ret randomHSBColor(); } |
146 | |
147 | void chooseColor enter { |
148 | inputText("Choose a color (RRGGBB)", color.getHexString(), voidfunc(S s) enter { |
149 | setColor(rgbFromHex(s)) |
150 | }); |
151 | } |
152 | |
153 | S switchableFields() { ret 'voiceMode; } |
154 | |
155 | void confirm enter { |
156 | dm_hideYesPopupForComponent(btnSave); |
157 | simulateClick(btnSave); |
158 | } |
159 | |
160 | void talk { // ask about current color |
161 | setColor(color); |
162 | } |
163 | } |
Began life as a copy of #1020199
download show line numbers debug dex old transpilations
Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1020242 |
Snippet name: | Teach Colors To The Machine [OK] |
Eternal ID of this version: | #1020242/234 |
Text MD5: | 527635451bcb3840341f95d42b785c98 |
Transpilation MD5: | bc3bb3f02c202ab1b4132fab96e307f2 |
Author: | stefan |
Category: | javax / stefan's os |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-12-14 19:43:57 |
Source code size: | 7709 bytes / 163 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 715 / 3688 |
Version history: | 233 change(s) |
Referenced in: | [show references] |